How to Change Add to cart text WooCommerce
For a better User Experience in your WooCommerce Store, you should let your user know which product has already been added to the cart, which has not been added, and if the user wants to add them again to double added product quantity. We can change Add to cart text WooCommerce after the product is added to the cart in two ways, one is by adding a plugin, which easiest way and another is by adding a PHP code snippet, where we will add a hook, and check if the product is already added to cart or not, based on we will change add to the cart button text.
Method 1: Change Add to Cart text by Plugin
Add to Cart Button Custom Text
There are four types of products you can add to WooCommerce. Simple Product, Affiliate product, Grouped product, Variable product. These plugins allow you to rename different product types and add to cart button text with different text. you can add emojis to the add to cart button. but with this plugin, you can’t add different text to the button after the user adds a product to the cart.
WooCommerce Custom Add To Cart Button
By using this plugin you can change add to cart text with the following features:
# you can change add to the cart button text.
# you can add an icon to add to cart button.
# you can remove text and only add an icon.
# you have full control over add to cart button text, you customize its text color, size, margin, and padding.
Method 2: Change Add to Cart text by PHP code snippet
This PHP code is can change all types of product button text, We have to just add this snippet in our theme functions.php or in our custom-made plugin.
This code is for a single product page:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'thedailywp_custom_add_cart_button_single_product', 9999 ); function thedailywp_custom_add_cart_button_single_product( $label ) { $label = 'Buy it now'; return $label; }
This Code is for the Product loop on the Shop page and product archive page:
add_filter( 'woocommerce_product_add_to_cart_text', 'thedailywp_custom_add_cart_button_loop', 9999, 2 ); function thedailywp_custom_add_cart_button_loop( $label, $product ) { if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() ) { $label = 'Buy it now'; } return $label; }
if you want to change add to cart text after the product is added to the cart, you can follow this PHP
code snippet.
for a single product page to change add to cart text is added to cart:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'thedailywp_custom_add_cart_button_single_product', 9999 ); function thedailywp_custom_add_cart_button_single_product( $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 = 'Product Already in Cart.'; break; } } } return $label; }
For product loop in shop page, product category page, and product archive page:
add_filter( 'woocommerce_product_add_to_cart_text', 'thedailywp_custom_add_cart_button_loop', 9999, 2 ); function thedailywp_custom_add_cart_button_loop( $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 = 'Product Already in Cart.'; break; } } } } return $label; }
If you want to change specific product add to cart text follow this PHP snippet:
add_filter( 'woocommerce_product_add_to_cart_text', 'thedailywp_custom_add_cart_button_loop', 9999, 2 ); function thedailywp_custom_add_cart_button_loop( $label, $product ) { if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() ) { $productID = "123"; if ( $productID == $_product->get_id() ) { $label = 'Product Already in Cart.'; break; } } return $label; }
Change the productID variable with your product id. if you don’t know how to get a product id, check out this tutorial, How can I get page/post ID in WordPress.
Conclusion
I try to add both ways to change add to cart text. if you still can’t manage to do it, let me know in the comment section below. we can discuss and find a solution. or send me a message by contact us page, you can hire some WooCommerce expert to do your job.
if you like this tutorial please like our Facebook page and Subscribe to our Youtube Channel