• I have a photography site and want to replace all images marked as sensitive to non logged-in users. I’ve sort of got it working on the main product page but the thumbnail appears underneath as the normal image, and I can’t figure out how to replace the image in the main shop page. Here is the code I have so far, can anyone assist please?

    
    function replace_featured_image($html, $attachment_id ) {
    
        $show = get_post_meta( get_the_ID(), 'sensitive_product', true );
    	if( $show ) {
    		remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
    		remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
    
    		if( ! is_user_logged_in() ) {
    				$img1 = get_stylesheet_directory_uri();
    				$img2 = "/images/blur.jpg";
    				echo '<img src="'.$img1.$img2.'">';
    				
    			} 
    			else {
    				add_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
    			};
    	}
    	
    	return $html;
    	
    }
    
    function replace_thumbnail_image($html, $attachment_id ) {
    
        $show = get_post_meta( get_the_ID(), 'sensitive_product', true );
    	if( $show ) {
    
    	remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
    		if( ! is_user_logged_in() ) {
    			add_action('woocommerce_before_shop_loop_item_title', 'new_woocommerce_template_loop_product_thumbnail', 10);
    								
    			} 
    			else {
    				return $html;
    			}
    	}
    	
    	return $html;
    	
    }
    
    function new_woocommerce_template_loop_product_thumbnail() {
    	$img1 = get_stylesheet_directory_uri();
    				$img2 = "/images/blur.jpg";
    				echo '<img src="'.$img1.$img2.'">';
    }
    
    add_filter('woocommerce_single_product_image_thumbnail_html', 'replace_featured_image', 10, 2);
    
    add_filter('woocommerce_placeholder_img_src', 'replace_thumbnail_image', 10, 2);
    
    • This topic was modified 4 years, 6 months ago by Comclusive.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support RK a11n

    (@riaanknoetze)

    Hi there,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Thread Starter Comclusive

    (@comclusive)

    Thanks. I’ve got most of the way as a non coder so didn’t really see it as complex. For now I’ve simply hidden the product if it’s marked as sensitive. This is quite a fundamental requirement and given all the major sites already do this I would imagine it should be easily achievable to overlay or replace an image dynamically.

    Thanks again.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Replace images in Woocommerce based on checkbox’ is closed to new replies.