WooCommerce Rename ‘Add to Cart’ Button if product already at Cart

Your customers are adding their favourite products in the cart. However, at times, you need to communicate with your customers if they are re-adding any product to the cart. It is an important UX element. This element informs the users when re-adding or increasing the quantity from the Shop/Loop/Category and product pages. In this tutorial, we will learn WooCommerce Rename ‘Add to Cart’ Button if product already at Cart. 

Further, the ‘Add to Cart’ button has 2 filters. One is for a single product page whereas another is for pages like a shop. So, we need to focus on both these elements. You will be required to ‘filter’ the label if the product is already in the cart and return it back to WooCommerce. 

Image indicating WooCommerce Rename ‘Add to Cart’ Button if product already at Cart

PHP Snippet: WooCommerce Rename ‘Add to Cart’ Button if product already at Cart

// Part 1
// Edit Single Product Page Add to Cart
add_filter( 'woocommerce_product_single_add_to_cart_text', 'phpsof_custom_add_cart_button_single_product' );
function phpsof_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', 'phpsof_custom_add_cart_button_loop', 99, 2 );
function phpsof_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 the snippet?

You can place it either at the bottom of your child theme functions.php file(delete “?>”, if you have it). Or else, CSS can go in your child theme style.css file. 

Also Read, A Complete Guide on WooCommerce Add to Cart Plus & Minus Buttons

WooCommerce How to Remove Product from Cart Programmatically