Back to Top

How to Rename WooCommerce Add To Cart Button If the Product is Already Added to the Cart?

Updated 16 August 2023

To Rename WooCommerce Add To Cart Button If The Product has already been added to The Cart, you can use a custom function or a custom WooCommerce plugin.

Here’s a step-by-step guide on how to rename add to cart button if the product is already added to the cart in WooCommerce:

1. About Add To Cart Button In WooCommerce

The “Add to Cart” button label comes with a few filter hooks, one for a single product page and another for other pages such as the Shop.

So all we have to do is target those two hooks. If the product is already in the cart, we’ll “filter” the label text and return it to WooCommerce.

2. Create a custom plugin or use your theme’s functions.php file in WooCommerce

<?php
/**
 * Plugin Name:Change "Add to Cart" Button Label if Product Already Cart
 * Description: How To Rename Add To Cart Button If The Product already added to The Cart In WooCommerce.
 * Plugin URI: https://webkul.com/
 * Version: 1.0.0
 * Author: Webkul
 * Author URI: https://webkul.com/
 * Text Domain: webkul
 */

3. Rename the button on the Single Product page In WooCommerce

Get the add to cart button text for the single page and rename them with the help of woocommerce_product_single_add_to_cart_text filter.

Searching for an experienced
Woocommerce Company ?
Find out More
/**
 * Rename the button on the Product page
 *
 * @param mixed $label
 * @return mixed
 */
function wk_product_page_add_cart_button( $label ) {
	if ( WC()->cart && ! WC()->cart->is_empty() ) {
		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?', 'webkul' );
				break;
			}
		}
	}
	return $label;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'wk_product_page_add_cart_button', 9999 );
woocommerce add to cart button rename

4. Rename the button on the Shop page In WooCommerce

Get the add to cart button text for the shop page with the help of woocommerce_product_add_to_cart_text filter.

/**
 * Rename the button on the Shop page
 *
 * @param mixed $label
 * @param mixed $product
 * @return mixed
 */
function wk_shop_page_add_cart_button( $label, $product ) {
	if ( $product->get_type() === 'simple' && $product->is_purchasable() && $product->is_in_stock() ) {
		if ( WC()->cart && ! WC()->cart->is_empty() ) {
			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?', 'webkul' );
					break;
				}
			}
		}
	}
	return $label;
}

add_filter( 'woocommerce_product_add_to_cart_text', 'wk_shop_page_add_cart_button', 9999, 2 );
woocommerce add to cart button rename

Support

Thank you for reading this dev article, for any technical assistance, please reach us by email at [email protected]

Also, you may Hire WooCommerce Developers to work on your projects.

Have a Great Day Ahead! See you in the next post. Keep reading 🙂

. . .

Leave a Comment

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


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home