Replace images in Woocommerce based on checkbox
-
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);
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Replace images in Woocommerce based on checkbox’ is closed to new replies.