Posted on

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

When talking about UX, or for very specific WooCommerce shops, you might need to “communicate” to 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, “filter” the label text in case the product is already in the Cart, and return that back to WooCommerce. If this looks like Japanese to you don’t worry – simply copy/paste the snippet below!

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

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


/**
 * @snippet       Change "Add to Cart" Button Label if Product Already @ Cart
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @source        https://businessbloomer.com/?p=73974
 * @author        Rodolfo Melogli
 * @testedwith    Woo 3.3.4
 */

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

add_filter( 'woocommerce_product_single_add_to_cart_text', 'bbloomer_custom_add_cart_button_single_product' );

function bbloomer_custom_add_cart_button_single_product( $label ) {
	
	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?', 'woocommerce');
		}
	}
	
	return $label;

}

// Part 2
// Edit Loop Pages Add to Cart

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

function bbloomer_custom_add_cart_button_loop( $label, $product ) {
	
	if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() ) {
		
		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?', 'woocommerce');
			}
		}
		
	}
	
	return $label;
	
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

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

  1. 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

  2. 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

  3. 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

  4. work only after refresh the page

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

  5. 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 ๐Ÿ™‚

  6. 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 ๐Ÿ™‚

  7. You are a great !!!

    Thank you

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.