最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Woocommerce - Default product image by user role

programmeradmin1浏览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

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

发布评论

评论列表(0)

  1. 暂无评论