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

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 );
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.

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.