This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

For Variable Products display parent image if there is no specific image

Home Forums Product Support Forums Ajax Search Pro for WordPress Support For Variable Products display parent image if there is no specific image

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #22656
    sonymerlinsonymerlin
    Participant

    We have variable products in the Woocommerce shop. Some variations don’t have images. We need to display the parent product image for those variations.

    Please let me know if this is possible.

    #22662
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi!

    It might be possible via using a small custom code snippet. Try add this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_filter( 'asp_results', 'asp_get_main_prod_img', 10, 1 );
    function asp_get_main_prod_img($results) {
    	foreach ( $results as $k => &$r ) {
    		if ( isset($r->post_type) && $r->post_type == 'product_variation' ) {
    			$wc_prod_var_o = wc_get_product( $r->id );
    			$img = wp_get_attachment_image_src(
    				get_post_thumbnail_id($wc_prod_var_o->get_parent_id()),
    				'single-post-thumbnail'
    			);
    			if (isset($img, $img[0]) && !is_wp_error($img))
    				$r->image = $img[0];
    		}
    	}
    	return $results;
    }

    This should hopefully resolve the issue.

    #22663
    sonymerlinsonymerlin
    Participant

    Thank you Ernest. The issue is resolved.

    Best regards,
    -Sony

    #22664
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.