WooCommerce: Rename “Add to Cart” Button if Product Already @ Cart

When talking about UX, or for very specific WooCommerce shops, you might need to tell the user a product is already in the Cart before re-adding it or increasing its quantity from the Shop/Category/Loop and Single Product pages.

The “Add to Cart” button label comes with a filter (actually 2 filters, one for the Single Product page and another for the other pages such as Shop), so all we need to do is targeting those two hooks. We will “filter” the label text in case the product is already in the Cart, and return that back to WooCommerce. If this sounds like Japanese to you (hey, unless you’re from Japan!) don’t worry – simply copy/paste the snippet below as per below instructions.

Enjoy!

Change the WooCommerce Add to Cart button label if Product is already in the Cart

PHP Snippet: Rename WooCommerce “Add to Cart” Button if Product Already in Cart

/**
* @snippet       Change "Add to Cart" Button Label if Product Already @ Cart
* @how-to        Get CustomizeWoo.com FREE
* @author        Rodolfo Melogli
* @compatible    WC 5.0
* @community     https://businessbloomer.com/club/
*/

// Part 1
// Single Product Page Add to Cart

add_filter( 'woocommerce_product_single_add_to_cart_text', 'bbloomer_custom_add_cart_button_single_product', 9999 );

function bbloomer_custom_add_cart_button_single_product( $label ) {
   if ( WC()->cart && ! WC()->cart->is_empty() ) {
      foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
         $product = $values['data'];
         if ( get_the_ID() == $product->get_id() ) {
            $label = 'Already in Cart. Add again?';
            break;
         }
      }
   }
   return $label;
}

// Part 2
// Loop Pages Add to Cart

add_filter( 'woocommerce_product_add_to_cart_text', 'bbloomer_custom_add_cart_button_loop', 9999, 2 );

function bbloomer_custom_add_cart_button_loop( $label, $product ) {
   if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() ) {
      if ( WC()->cart && ! WC()->cart->is_empty() ) {
         foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values['data'];
            if ( get_the_ID() == $_product->get_id() ) {
               $label = 'Already in Cart. Add again?';
               break;
            }
         }
      }
   }
   return $label;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce Visual Hook Guide: Archive / Shop / Cat Pages
    I’ve created a visual HTML hook guide for the WooCommerce Archive Page (which is the same page for the Shop, Category, Tag pages). This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

42 thoughts on “WooCommerce: Rename “Add to Cart” Button if Product Already @ Cart

  1. Does this script work with the Woodmart theme?

    1. If they use WooCommerce add to cart buttons, it will work

  2. Hey,
    First, I love this code, but it only works on single product and not single variation buttons. I am not sure way.
    I have mostly variation products in my Woocommerce site.

    1. Hello Tarzine, thanks so much for your comment! Yes, this is definitely possible with a combination of custom jQuery and PHP, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi,
    What if I want the Add to Cart button to be disabled after adding to the cart, how can I do that?

    1. Hi Daveed, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Works like a charm . Thakns mate!

  5. I am getting an error after I added this to my functions php file, like the whole website wouldn’t worry
    maybe syntax error? not sure
    please check it again, thanks

    1. What error do you get (please check your error logs)?

      1. Unexpected #e0f

        1. Sorry, try this revised version, I forgot a }

  6. I get “Uncaught Error: Call to a member function get_cart() on null” in Gutenberg editor when use this code.

    1. Thanks! I’ve revised the snippet, please test

  7. Hi
    thanks for this great code.
    but this code doesn’t work for woocommerce add_to_cart ajax button.
    do you have any solution for this problem.

    1. Not sure, sorry

      1. Hi Rudolfo

        This snippet still works perfectly, however, if you use AJAX for add to cart buttons, which was the case for my theme, then the button text doesn’t change before the page is being refreshed manually.

        I believe this is because the product is added to the minicart widget.

        The workaround for me was to go into theme settings and deactivate the AJAX add to cart.
        Another option would be to add a function which auto-refreshes after the button click.

        Hope this can help others with similar challenge.

  8. Can you also provide a code that deactivates the click function of the add to cart button in shop and product page when the product is already in the cart? (e.g. for downloadable product)

    1. No, sorry

  9. Is there a way to redirect to the cart page if the product is already in the cart?

    1. Hey Axle, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  10. Exactly what I needed and worked like a dream.

    Thank you for all your efforts Rodolfo. ๐Ÿ˜€

    1. Thank you Nicole!

  11. Hello Rodolfo,

    Thank you very much for sharing this tip ๐Ÿ™‚

    Sadly when using a cache plugin (what often happens), this seems to not work correctly even if “Ajax Add to Cart” is disabled. Example: when the product is removed from the shopping cart, “already in cart” is still displayed on the product page or on the shop page (even after a page refresh).

    Do you have an idea?

    Many thanks in advance.

    1. Hey Svan, works for me with SiteGround cache… maybe ask your cache plugin support?

  12. Thanks man.

    But how can you change the “add to cart button” to “already ordered” if you only wish to have one product per person?

    1. Hello Rexy – thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  13. Hi, how to change to the red color “Already in Cart. Add again?” text? Or do you have link for that please..

    1. Hey Dannie, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  14. hello

    work great

    can replace add to cart link after add to cart. with change label. change link to pay (checkout page?)

    1. Hey Ahmad, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  15. Hi,
    The only problem with this snippet is when you remove an item from cart the already in cart button will stay in place, even if item is removed.
    Michel.

    1. Good point Michel, I guess the snippet could be “Ajaxified” in order to detect Ajax calls such as the remove from cart. Unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  16. work only after refresh the page

    1. Possibly you need to disable the “Ajax Add to Cart”, good point ๐Ÿ™‚

  17. Hello, Does this code serve for variable products?
    I have only variable products in my shop.

    1. Hey Nerea, I haven’t tested it with variable products! Give it a go and let me know ๐Ÿ™‚

  18. Nice one, definitely will use it!

    One quicky: the text “Already in Cart. Add again?” is not in the standard woocommerce language file, is it? Should I put a new entry into it to use this text? Or some other place? What’s the recommended method?

    1. Hey Peter, thanks for your comment! As you can see from the snippet I added it to the ‘woocommerce’ textdomain, so you can now translate that with any translation plugins ๐Ÿ™‚

  19. You are a great !!!

    Thank you

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *