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        Get CustomizeWoo.com FREE
 * @source        https://businessbloomer.com/?p=73974
 * @author        Rodolfo Melogli
 * @compatible    WC 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

// 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;
	
}

Share:

Leave a Reply