Removing Add To Cart from WooCommerce Loop only on one product

Trying to remove the add to cart button only from product in the product loop and add some text instead.

The following code does remove the button, but also from all buttons after the condition has been validated:

Read More
function remove_purchase_buttons_from_loop() {

if( $product->id == $_product->id ) {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    add_action( 'woocommerce_after_shop_loop_item',  'level_already_in_cart_notice', 10 );
       }

}

add_action ('woocommerce_before_shop_loop_item', 'remove_purchase_buttons_from_loop'));

I understand why this happening but what is the right solution

Related posts

1 comment

  1. Try this

    if(is_single()) {
      function remove_purchase_buttons_from_loop() {
    
         if( $product->id == $_product->id ) {
             remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
             add_action( 'woocommerce_after_shop_loop_item',  'level_already_in_cart_notice', 10 );
           }
       }
       add_action ('woocommerce_before_shop_loop_item', 'remove_purchase_buttons_from_loop'));
    } else {
       // your code
    }
    

    I hope this will help you.

Comments are closed.