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.
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:
The Add to Cart button text is thus changed for products which are already in the cart.
but how to change the button text that shows right after you’ve added the product to the Cart? The default: Add to cart with a checkmark.
Is that possible as well?
Big thx!
Welcome! 🙂