WooCommerceWordPress

How to display product images to logged-in users only in WooCommerce

0
580
2019 / 01 / 24

At first, you will need a child theme for your parent theme.

Open functions.php of your child theme and add the following code:

<?php
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );

// conditional logic function for logged-in users
function logged_in_images(){
    // if user is Logged-in, display original product image
    if ( is_user_logged_in() ) {
        echo woocommerce_show_product_images(); 
    } 
    // if user is not logged in, display the placeholder image instead
    else {
        echo '<img src="https://via.placeholder.com/150">';
    };
}
 
add_action( 'woocommerce_before_single_product_summary', 'logged_in_images', 20 );
?>

Be sure to change the placeholder with your own default image which you want to select.