- This topic has 5 replies, 2 voices, and was last updated 5 years ago by
David.
-
AuthorPosts
-
March 15, 2021 at 8:19 am #1696167
Raffaele Salvemini
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.
March 15, 2021 at 12:34 pm #1696405David
StaffCustomer SupportHi there,
checking the woocommerce plugin:
You need to remove it from the
woocommerce_after_shop_loop_item_titlehookMarch 16, 2021 at 12:14 am #1696837Raffaele Salvemini
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?
March 16, 2021 at 3:12 am #1697021David
StaffCustomer SupportOoops… 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.
March 18, 2021 at 12:23 am #1699840Raffaele Salvemini
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!
March 18, 2021 at 2:31 am #1699961David
StaffCustomer SupportAah – sorry i misread the topic. Glad to see you got it resolved!
-
AuthorPosts
- You must be logged in to reply to this topic.