Run code snippet on 1 specific Woocommerce product page
-
Hi there ??
I found the code below and applied it to my store using the Code Snippets plugin. It works perfectly well, but affects all products in my store.
add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2); function remove_featured_image($html, $attachment_id ) { global $post, $product; $featured_image = get_post_thumbnail_id( $post->ID ); if ( $attachment_id == $featured_image ) $html = ''; return $html; }
I found this thread on the Code Snippets wordpress support forum, where Shea Bunge gave an answer as to how to do what I’m trying to do, suggesting that it was “a little trickier, but still doable”. The suggested code to add at the beginning of a snippet to affect only one product was as follows:
$allowed_products = [ 'product-slug', 42, 'Product Name' ]; if ( ! is_singular( [ 'product' ] ) || ! is_single( $allowed_products ) ) return;
He also said, “For this one, you need to set $allowed_products to be an array of the product IDs, slugs, or titles for the products you want to run the snippet on.”
I’m not very comfortable with PHP yet, but by that instruction I understood to make the code for my snippet as follows:
$allowed_products = [ 'clair-de-lune-course', 51213, 'Clair de Lune Course' ]; if ( ! is_singular( [ 'product' ] ) || ! is_single( $allowed_products ) ) return; add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2); function remove_featured_image($html, $attachment_id ) { global $post, $product; $featured_image = get_post_thumbnail_id( $post->ID ); if ( $attachment_id == $featured_image ) $html = ''; return $html; }
However it didn’t work ?? The snippet still affects all products on my site. Nothing breaks with this snippet so that’s great ?? But I’m clearly getting something wrong… Anyone able to help?
Thanks so much!
KevinThe page I need help with: [log in to see the link]
- The topic ‘Run code snippet on 1 specific Woocommerce product page’ is closed to new replies.