Search the site:

Home Blog How to rename the Add to Cart button if the product is already added to cart in WooCommerce

How to rename the Add to Cart button if the product is already added to cart in WooCommerce

You may have encountered while shopping on e-commerce websites how sometimes, a product gets added more than once to the shopping cart by accident, thereby increasing the quantity of the same product. Many times, this quantity and price may even be overlooked during the Checkout process, and you may get caught in a different hassle trying to place a return request on the additional product. So how can you make this experience better for your customers? This can be done simply by renaming the Add to cart button if the product is already added to the cart. Let’s explore how you can do this in WooCommerce.

rename Add to Cart button if the product is already added to cart in WooCommerce - Shopping Cart
WooCommerce Basket / Cart Page

The Add to cart button appears in two places on the WooCommerce store viz. the Product page and the Shop page. To modify this button in these two places, we will need to use two different hooks in our code – the woocommerce_product_single_add_to_cart_text and woocommerce_product_add_to_cart_text respectively.

//Rename the button on the Product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'ts_product_add_cart_button' );
 
function ts_product_add_cart_button( $label ) {
    
   foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
      $product = $values['data'];
      if( get_the_ID() == $product->get_id() ) {
         $label = __('Already added to Cart. Add again?', 'woocommerce');
      }
   }
    
   return $label;
 
}
 
//Rename the button on the Shop page 
add_filter( 'woocommerce_product_add_to_cart_text', 'ts_shop_add_cart_button', 99, 2 );
 
function ts_shop_add_cart_button( $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 added to Cart. Add again?', 'woocommerce');
         }
       }    
   }
    return $label;    
}

This code, when added to the functions.php file of your child theme will change the text of the button in both places:

rename Add to Cart button if the product is already added to cart in WooCommerce - Add to cart button text changed on Product page
Product page

 

rename Add to Cart button if the product is already added to cart in WooCommerce - Add to cart button text changed on Shop page
Shop Page

The Add to Cart button text is thus changed for products which are already in the cart.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Leave a Reply

avatar

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

  Subscribe  
Notify of
from purchased

Subscribe Now