Site logo

Archived topic

Remove to cart button from a specific product category

5 replies · Started by Raffaele Salvemini on March 15, 2021

Viewing posts 1–6 of 6

Hello everybody! I'm developing an ecommerce with a generatepress child theme. I'm trying to remove the add to cart button from a specific product category through this php function:

add_action( 'woocommerce_before_shop_loop_item', 'remove_product_description_add_cart_button', 10 );
function remove_product_description_add_cart_button() {
if (has_term('immagini', 'product_cat', get_the_id() ) ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
}

}

As you can see in the screenshot I currently have two buttons, the woocommerce add to cart button (which I'm trying to remove) and another one I implemented.
Can anyone help me? The code I pasted removes every button, without considering the if statement that should check the product category.

Hi David,

thank you for your answer.

I tried to use the hook you suggested but it removes the price (which I need to keep), not the button.

Any other ideas?

Ooops... don't know what i was thinking yesterday ... sorry about that.

Your original function ie.

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);

works fine for me on my test server.

If thats not working then you may need to make that fire hire up the execution order like so:

add_action( 'wp', function() {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
} );

If that still doesn't work then their has to be some other function/plugin that has already moved the button to different hook/priority.

Thank you David for your support.

The code I was using was fine, I just needed an else statement:

function remove_product_description_add_cart_button() { // function for deleting ...
$terms = get_the_terms( get_the_ID(), 'product_cat' );
foreach ( $terms as $term ) {
if (has_term ('ortofrutta', 'product_cat', get_the_id ())) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
}
else{
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
}
}
}

add_action( 'woocommerce_after_shop_loop_item_title', 'remove_product_description_add_cart_button', 10 );

This is the correct code.

Bye!

Aah - sorry i misread the topic. Glad to see you got it resolved!

This archived topic is closed to new replies.