Yotam Dahan
Yotam Dahan

Reputation: 699

How to display different images for non logged users in WooCommerce shop page?

I'm using WooCommerce under the WordPress platform to create my e-commerce shop, but I want to display the archive shop page images for logged-in users only.

Non-logged-in users will see placeholders that prevents them to see the images of the shop.

I have followed this tutorial which seems exactly what I'm looking for, but no visual results accord, I believe this hook is either irrelevant or deprecated.

//Display different images for non-logged in users
add_filter( 'woocommerce_placeholder_img_src', 'growdev_custom_woocommerce_placeholder', 10 );

function growdev_custom_woocommerce_placeholder( $image_url ) {
    if ( is_user_logged_in() ) {
        return;
    }
  $image_url = 'https://vps1420.secured-cloud.net/wp-content/uploads/2021/04/516.webp';  // change this to the URL to your custom placeholder
  return $image_url;
}

Right now the images continue to display as usual (no matter if I'm logged in or not).

How can I display different images for unregistered users in WooCommerce?

Upvotes: 1

Views: 278

Answers (1)

Bhautik
Bhautik

Reputation: 11282

You can use woocommerce_before_shop_loop_item_title action hook. first remove woocommerce_before_shop_loop_item_title hook then add that hook again and use is_user_logged_in() to check user logged in or not. code goes to your active theme functions.php file.

Use this woocommerce_get_product_thumbnail() funciton to get product thumbnail.

Use this wc_placeholder_img() funciton to get product placeholder image.

// Remove product images from the shop loop
function remove_shop_images() {
    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
}
add_action( 'after_setup_theme', 'remove_shop_images' );

function display_product_image_based_on_logged_in(){
    if( is_user_logged_in() ){
        echo woocommerce_get_product_thumbnail();
    }else{
        echo wc_placeholder_img();
    }
}
add_action( 'woocommerce_before_shop_loop_item_title', 'display_product_image_based_on_logged_in', 10 );

Upvotes: 2

Related Questions