Site logo

[Support request] Remove to cart button from a specific product category

Home Forums Support [Support request] Remove to cart button from a specific product category

Home Forums Support Remove to cart button from a specific product category

  • This topic has 5 replies, 2 voices, and was last updated 5 years ago by David.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #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.

    #1696405
    David
    Staff
    Customer Support

    Hi there,

    checking the woocommerce plugin:

    https://github.com/woocommerce/woocommerce/blob/23710744c01ded649d6a94a4eaea8745e543159f/templates/content-product.php#L52-L57

    You need to remove it from the woocommerce_after_shop_loop_item_title hook

    #1696837
    Raffaele 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?

    #1697021
    David
    Staff
    Customer Support

    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.

    #1699840
    Raffaele 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!

    #1699961
    David
    Staff
    Customer Support

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

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.