Woocommerce - Default product image by user role

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745637242a4637468.html

相关推荐

  • Woocommerce - Default product image by user role

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

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信