skip to Main Content

I would like to display a different thumbnail for some of my products, if they appear in specific categories.

I have removed the current thumbnails from the specific category and I am using a custom field to pull the new thumbnail for the products I want to replace, which is working great. However, when I try to call the normal thumbnail for the remaining products it doesn’t work – any ideas?

add_action( 'woocommerce_before_main_content', 'remove_test_category_thumbnails', 10 );

function remove_test_category_thumbnails() {
    if (is_product_category('test-category')) {
        remove_action('woocommerce_before_shop_loop_item_title' , 'woocommerce_template_loop_product_thumbnail' , 10);
    }
}

add_action( 'woocommerce_before_shop_loop_item_title', 'add_test_category_thumbnails', 10 );

function add_test_category_thumbnails() {
    global $post;
    $testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true);
    if (is_product_category('test-category') && (isset($testCatThumb)) ) {
        echo wp_get_attachment_image( $testCatThumb );
    }
    else 
        echo woocommerce_get_product_thumbnail();
    }
}

2

Answers


  1. You need to echo the woocommerce_get_product_thumbnail. check below code.

    add_action( 'woocommerce_before_main_content', 'remove_test_category_thumbnails', 10 );
    function remove_test_category_thumbnails() {
        if (is_product_category('test-category')) {
            remove_action('woocommerce_before_shop_loop_item_title' , 'woocommerce_template_loop_product_thumbnail' , 10);
        }
    }
    
    add_action( 'woocommerce_before_shop_loop_item_title', 'add_test_category_thumbnails', 10 );
    function add_test_category_thumbnails() {
        global $post;
        $testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true); 
        if( is_product_category( 'test-category' ) && ( isset( $testCatThumb ) ) ) {
            echo wp_get_attachment_image( $testCatThumb );
        }else{
            echo woocommerce_get_product_thumbnail();
        } 
    }
    
    Login or Signup to reply.
  2. The following function expects a post id, is that what you are passing it?

    wp_get_attachment_image();
    

    as part of your if condition, you could check the value is of the correct value type:

    is_numeric($testCatThumb);
    

    I tend to create readable variables, rather than having horrible conditions you have to parse with your brain every time you read it:

    function add_test_category_thumbnails() {
        global $post;
    
        $testCatThumb        = get_post_meta( $post->ID, 'step_dad_mug', true);
        $isPostId            = !empty($testCatThumb) && is_numeric($testCatThumb);
        $isValidAttachmentId = is_product_category('test-category') && $isPostId;
        $image               = $isValidAttachmentId ? wp_get_attachment_image($testCatThumb) : '';
    
        // display custom image, or fallback to woocommerce thumbnail
        echo !empty($image) ? $image : woocommerce_get_product_thumbnail();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search