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

This topic contains 3 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 3 years, 3 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #22656
    sonymerlin
    sonymerlin
    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 Marcinko
    Ernest 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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #22663
    sonymerlin
    sonymerlin
    Participant

    Thank you Ernest. The issue is resolved.

    Best regards,
    -Sony

    #22664
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.