0

I would like that only the user role 'customer' see the product images and all the other roles see a default product image.

I am searching to do this with no luck. Can someone help me please?

I managed to do the same for the product prices and this is working correctly

The code I added to functions.php is:

function ace_hide_prices_guests( $price ) {
    if(current_user_can('customer')) {
           return $price;
        }
        return '';

}
add_filter( 'woocommerce_get_price_html', 'ace_hide_prices_guests' );

Thanks

  • At first glance it looks like Woocommerce uses the post thumbnail as the main product image. You can probably hook get_post_metadata to refuse to read metadata _thumbnail_id if the user isn't a customer. And I'd probably put || is_admin() on all of these checks. – Rup Apr 1 at 9:44
  • But then there's the gallery too etc. It may be simpler to just override Woocommerce's product templates to do the checks there rather than trying to limit the data that's passed to them. – Rup Apr 1 at 9:45
0
/**
 * Only allowing the customer and admin user role to view the product images
 *
 * @param $value
 *
 * @return bool
 */
function woocommerce_product_get_image_id_callback( $value ) {

    global $current_user;

    if (
        in_array( 'customer', (array) $current_user->roles )
        || in_array( 'administrator', (array) $current_user->roles )
    ) {
        return $value;
    } else {
        return false;
    }

}

add_filter( 'woocommerce_product_get_image_id', 'woocommerce_product_get_image_id_callback', 10, 1 );

Copy the above code and paste it into you functions.php file of your child themes

  • But if I want that the images are also displayed for the admin I change the if to if ( in_array( 'customer', (array) $current_user->roles ) || is_admin() ) but then it doesn't work anymore. Anny ideas? – Kevin Sneyers Apr 1 at 14:05
  • 1
    @KevinSneyers I have updated the code now the customer and the admin both user role can view the product images – raftaar1191 Apr 2 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.