Akash Agrawal
Akash Agrawal

Reputation: 2299

Hide add to cart button and add custom content after it in Woocommerce

I've added a 'Customize Now' button and few dropdowns after the 'add to cart' button using woocommerce_after_add_to_cart_button.

But now when I try to hide the 'add to cart' button (which I have to for a specific scenario my website need) using woocommerce_is_purchasable, the 'Customize Now' button and dropdowns are also hidden. Is there any proper order/sequence to do this?

Filter to add the Customize button and dropdowns:

add_action('woocommerce_after_add_to_cart_button', array($this, 'pn_get_calc_and_customize_btn'));

Filter to remove the add to cart button:

add_filter('woocommerce_is_purchasable', array($this, 'pn_hide_add_to_cart_button'), 10, 2);

Upvotes: 1

Views: 1118

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253784

As add-to-cart templates display condition is:

if ( ! $product->is_purchasable() ) {
    return;
}

2 ways:

1) Use instead woocommerce_single_product_summary hook with a priority between 30 and 40:

add_action('woocommerce_single_product_summary', array($this, 'pn_get_calc_and_customize_btn'), 35 );

Then your function output code should be embedded in a custom <form> and you will need to add some more code to save the data in cart or elsewhere…


2) To remove cart button, use woocommerce_product_is_in_stock filter hook instead of woocommerce_is_purchasable so you will have to change a bit your hooked function code too...

add_filter('woocommerce_product_is_in_stock', array($this, 'pn_hide_add_to_cart_button'), 10, 2);

Upvotes: 2

Nick
Nick

Reputation: 14283

I have two suggestions here:

The first one will be to try to add the priority to your add_action() as well. As per documentation, the lower the number, the earlier the execution. I would try to add a greater priority to add_action() and try to force the woocommerce_after_add_to_cart_button to be executed after your filter. However, I don't know if removing the button also inhibits the filter (it might be).

Another suggestion I may have is to override the default template for the page (i don't know if you're editing the shop page or the single_product page) and have some if{}else{} login in there to show hide buttons based on the situation.

I don't know if either of these solutions is any good for you but this was just my tough and how I would tackle it.

Hope it helps in any way

Upvotes: 1

Related Questions