• 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!
    Kevin

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    You just need to make sure the conditional check is inside the filter hook, not outside it:

    add_filter( 'woocommerce_single_product_image_thumbnail_html', function ( $html, $attachment_id ) {
    	global $post;
    
    	$allowed_products = [ 'clair-de-lune-course', 51213, 'Clair de Lune Course' ];
    
    	if ( ! is_singular( [ 'product' ] ) || ! is_single( $allowed_products ) ) {
    		return $html;
    	}
    
    	$featured_image = get_post_thumbnail_id( $post->ID );
    
    	if ( $attachment_id == $featured_image ) {
    		$html = '';
    	}
    
    	return $html;
    }, 10, 2 );

    Also, you don’t need the title, ID and slug in the array – just one piece of information per product will do.

Viewing 1 replies (of 1 total)
  • The topic ‘Run code snippet on 1 specific Woocommerce product page’ is closed to new replies.