WooCommerce : Change “Add to Cart” Text if Product Already in Cart.

When a product is in cart , woocomerce still show ‘add to cart’ text. There is no information on shop page or product page that ‘s this product is already in cart.

‘Add To Cart’ text comes from two filter one for shop page, and one for product single page.
For Loop Page /shop page use 'woocommerce_product_add_to_cart_text' Filter.

// Shop page
function ChangeAddToCartButtonTextLoop( $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;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'ChangeAddToCartButtonTextLoop', 99, 2 );

Example:


For Single Product Page use 'woocommerce_product_single_add_to_cart_text' Filter.

// Single Product page
function ChangeAddToCartButtonTextSingleProduct( $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;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'ChangeAddToCartButtonTextSingleProduct' );

Example:

Leave a Comment

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

Scroll to Top