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
You need to
echo
thewoocommerce_get_product_thumbnail
. check below code.The following function expects a post id, is that what you are passing it?
as part of your if condition, you could check the value is of the correct value type:
I tend to create readable variables, rather than having horrible conditions you have to parse with your brain every time you read it: