0

I want to disable the 'add to cart' button if the product is already added. I have managed to change the. test for an already added product so that it says 'Already added' for the button. The code or that is below. I just don't know how to disable that button in addition to that text change.

add_filter('woocommerce_product_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
add_filter('woocommerce_product_single_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );

function wc_product_add_to_cart_text( $text, $product ){

    $product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
    $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

    if ( $in_cart ) {
        $text = "Already added";
    }
    return $text;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
BruceyBandit
  • 3,978
  • 19
  • 72
  • 144

2 Answers2

1

When you're editing a product in WooCommerce it's possible to select "only sold individually". I don't know if that's what you're trying to achieve, but that might be a more suitable solution and less prone to bugs.

If you are not using the default shop page at all you should put a redirect at the shop page that always redirects the user to for example the page you are using for the shop.

Besides that, here you'll find a hook you can use to disable redirects for "Add to cart".

I found this code to check if a product is already in your cart. You can get the current product id from your body_class();. Just hook this function onto woocommerce_add_to_cart.

function woo_in_cart($product_id) {
    global $woocommerce;         
    foreach($woocommerce->cart->get_cart() as $key => $val ) {
        $_product = $val['data'];

        if($product_id == $_product->id ) {
            wp_redirect( $url );
        }
    }         
}
Tom
  • 387
  • 2
  • 12
  • Hey Tom, the issue I have is that I have already got the products set to max of 1 quantity. But what happens is that if the customer attempts to add the same product to cart, it takes them to a shop page we don't use with the message product is already in cart. That's why I was thinking of a solution so the customer does not go to that unused shop page. Would be good as an alternative to instead show that product is already in cart message on the cart page instead of the shop page if that's possible? – BruceyBandit Oct 16 '20 at 22:58
  • Are we ok to use SO chat so I can show you what I mean? I'm not a wordpress pro so you may know more on how to bypass this and potentially show the message in the cart – BruceyBandit Oct 16 '20 at 22:58
  • I can't chat yet, I need 4 extra reputation points for that. However I've updated my answer. – Tom Oct 16 '20 at 23:05
  • Ah ok cool, I do have a redirect plugin so I am planning to use the redirect, however I am thinking of using the redirect for the shop scenario except adding to cart if duplicate. I think if user adds same product in cart, if it send them to the cart page with the message, that would be good. Hope that makes sense – BruceyBandit Oct 16 '20 at 23:12
  • If you just redirect them to the cart directly after adding the product the first time that would probably fix your issue. I don't reckon any user would see the shopping cart, go back to the item and try to add it again. If you need the code to do this redirect, let me know and I'll update my answer. – Tom Oct 16 '20 at 23:18
  • Hey Tom, yeah if you don't mind doing the code to redirect after clicking add to cart button for a duplicate product and it takes the user to the cart page with a message displayed, that would be great. In the meantime I am using the redirectton plugin to redirect from shop page to product page if user clicks on product image or name. – BruceyBandit Oct 16 '20 at 23:52
  • 1
    Oh awesome. Thank you. Where did you find the path (path to the file containing the code) – BruceyBandit Oct 17 '20 at 00:19
  • I knew the hook and redirect function, but I found the code snippet to check for a product in your cart here: https://stackoverflow.com/questions/41262426/woocommerce-check-if-items-are-already-in-cart – Tom Oct 17 '20 at 00:23
  • add_action('woocommerce_add_to_cart', 'woo_in_cart', 1 ); function woo_in_cart($product_id) { global $woocommerce; foreach($woocommerce->cart->get_cart() as $key => $val ) { $_product = $val['data']; if($product_id == $_product->id ) { wp_redirect( $url ); } } } – BruceyBandit Oct 17 '20 at 00:34
  • Sorry above is not formatted, did what you mentioned, added the code to woo_commerce_add_to_cart and it still redirects me to shop. Do I need to add something else to get it to redirect to the cart page? Unless you expect me to change $url to a hard coded url? – BruceyBandit Oct 17 '20 at 00:35
  • You do need to replace $url with the url you want them to redirect to, in this case the url for your shopping cart. – Tom Oct 17 '20 at 00:41
0

Just add

ul.woocommerce-error {
display: none;

}

in your additional css.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 27 '22 at 01:57