How to Rename the Add to Cart Button When a Product Is Already in the Cart

How to Rename the Add to Cart Button When a Product Is Already in the Cart
On this page
  1. Why Flag Products Already in the Cart
  2. Single Product Page
  3. Shop/Category Loop Pages
  4. Troubleshooting
  5. Frequently Asked Questions
  6. Conclusion

To rename the Add to Cart button when a product is already in the cart, filter both woocommerce_product_single_add_to_cart_text and woocommerce_product_add_to_cart_text, looping through the current cart contents to check if the product’s ID is already present, then swap the label accordingly.

Why Flag Products Already in the Cart

  • It prevents accidental duplicate additions when customers forget they already added an item
  • It’s a small trust signal that your store’s cart state is accurate and responsive
  • It applies cleanly to both the single product page and shop/category loop with the same logic

Single Product Page

PHP
/** Change the single product Add to Cart label if it's already in the cart */
add_filter( 'woocommerce_product_single_add_to_cart_text', 'flag_button_if_already_in_cart_single', 9999 );

function flag_button_if_already_in_cart_single( $button_text ) {
    if ( WC()->cart && ! WC()->cart->is_empty() ) {
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( get_the_ID() === $cart_item['data']->get_id() ) {
                return 'In Your Cart — Add Another?';
            }
        }
    }
    return $button_text;
}
Rename the add to cart button when a product is already in the cart: the page before adding the code
Before
Rename the add to cart button when a product is already in the cart: the page after adding the code
After

Shop/Category Loop Pages

PHP
add_filter( 'woocommerce_product_add_to_cart_text', 'flag_button_if_already_in_cart_loop', 9999, 2 );

function flag_button_if_already_in_cart_loop( $button_text, $product ) {
    if ( 'simple' !== $product->get_type() || ! $product->is_purchasable() || ! $product->is_in_stock() ) {
        return $button_text;
    }

    if ( WC()->cart && ! WC()->cart->is_empty() ) {
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $product->get_id() === $cart_item['data']->get_id() ) {
                return 'In Your Cart — Add Another?';
            }
        }
    }

    return $button_text;
}

Troubleshooting

The Label Doesn’t Update Without a Page Refresh

These filters run on server-side render, so the label only updates on page load — for an instant AJAX-cart-style update without reloading, you’d need to also handle this via JavaScript listening to the added_to_cart event.

Variable Products Don’t Show the Flag Correctly

The loop example above intentionally limits itself to simple products, since matching “already in cart” for a variable product requires comparing the specific variation ID, not just the parent product ID — extend the check with get_variation_id() if you need that level of detail.

Frequently Asked Questions

Can I show the current quantity in the cart instead of a generic message?

Yes — grab $cart_item['quantity'] inside the loop and include it in the returned label, e.g. “2 in Your Cart — Add Another?”.

Will this work with AJAX add-to-cart enabled?

The initial label is correct on page load; for the label to update immediately after an AJAX add without a refresh, you’ll need a small JS snippet bound to the AJAX success event too.

Does this affect the actual add-to-cart functionality?

No — it only changes the button’s visible text; clicking it still adds another unit of the product to the cart as normal.

Conclusion

Renaming the Add to Cart button when a product is already in the cart takes two matching filters — one for the single product page, one for the loop — both built around a simple check of the current cart contents against the product ID being displayed.

See also How to Rename the Place Order Button in WooCommerce.

Prefer no code? All-in-One Utilities Woo Add-on handles this kind of change from a settings screen. Its Add to cart text and prices guide also shows how.

Leave a Reply

Schedule a call