Rename “Add to Cart” Button if Product Already at Cart

If Product already at WooCommerce Cart than change “Add to Cart” Button text and Displaying a different text instead of default “Add to Cart” text. So user can easily communicate with product. In Woocommerce it’s very easy. Woocommerce provide 2 filter hook for that. one filter hook for the Single Product page and another filter hook for the other pages such as Shop.

So first, we want to check if the product is already in user cart. Then according to the status of the product, we want to return the text for the Add to Cart button.

Add this code in your theme’s functions.php file of your active child theme (or theme).

add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_cart_button_single_product' );
 
function 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;
 
}
 
// Change the add to cart text on product archives
 
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_cart_button_loop', 99, 2 );
 
function 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;
     
Scroll to Top