最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Woocommerce - Default product image by user role

matteradmin7PV0评论

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

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

Share Improve this question edited Apr 1, 2019 at 9:03 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Apr 1, 2019 at 8:53 Kevin SneyersKevin Sneyers 31 bronze badge 2
  • 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 Commented Apr 1, 2019 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 Commented Apr 1, 2019 at 9:45
Add a comment  | 

1 Answer 1

Reset to default 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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far