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

plugins - How to use filter to disable adding a product to wishlist?

programmeradmin4浏览0评论

I am using a plugin on my website that made the following filter available:

Added filter ‘tinvwl_allow_addtowishlist_single_product’ that helps to show/hide the “Add to Wishlist” button for specific products on a single products page

The filter comes into place here:

if ( empty( $this->product ) || ! apply_filters( 'tinvwl_allow_addtowishlist_single_product', true, $this->product ) ) {
    return;
}

I would now like to add this filter to a single product page to disable the "add to wishlist" functionality in some cases, but

  • Where should I hook this filer? Should this be hooked at woocommerce_before_single_product as found on / ?
  • Which arguments do I need in order to pass the false option?
  • How to add the filter statement correctly in my functions.php file?

My current attempt:

function disable_add {
    $allow = false;
    $product = $product->get_id();
    return $allow;
    return $product;
}

add_filter( 'tinvwl_allow_addtowishlist_single_product_summary','disable_add', 15, 2 );

I am using a plugin on my website that made the following filter available:

Added filter ‘tinvwl_allow_addtowishlist_single_product’ that helps to show/hide the “Add to Wishlist” button for specific products on a single products page

The filter comes into place here:

if ( empty( $this->product ) || ! apply_filters( 'tinvwl_allow_addtowishlist_single_product', true, $this->product ) ) {
    return;
}

I would now like to add this filter to a single product page to disable the "add to wishlist" functionality in some cases, but

  • Where should I hook this filer? Should this be hooked at woocommerce_before_single_product as found on https://businessbloomer/woocommerce-visual-hook-guide-single-product-page/ ?
  • Which arguments do I need in order to pass the false option?
  • How to add the filter statement correctly in my functions.php file?

My current attempt:

function disable_add {
    $allow = false;
    $product = $product->get_id();
    return $allow;
    return $product;
}

add_filter( 'tinvwl_allow_addtowishlist_single_product_summary','disable_add', 15, 2 );
Share Improve this question edited Apr 11, 2019 at 10:58 BarrieO asked Apr 11, 2019 at 9:12 BarrieOBarrieO 992 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I was almost correct in my attempt.

  • I forgot to add the arguments in my function definition, which is important of course.
  • I took the wrong function name, the _summary at the end was not needed.

The correct code is the following:

/* DISABLE ADD TO WISHLIST FOR USERS WHO AREN'T ADMIN OR SHOP MANAGER */
/* --- */
function remove_add_button ( $allow, $product ) {
    $allow = false;
    $product = $product->get_id();
    return $allow;
    return $product;
}

$user = wp_get_current_user();
if ( ! in_array( 'administrator', (array) $user->roles ) && ! in_array( 'shop_manager', (array) $user->roles ) ) {
    add_filter( 'tinvwl_allow_addtowishlist_single_product','remove_add_button', 15, 2 );
}

As you can see I have also added a condition on the user role, so only admins & shop managers can use the button.

发布评论

评论列表(0)

  1. 暂无评论