Do you need to change the text that displays on the WooCommerce “Add To Cart” button? We needed to recently and in this snippet, we will show you exactly how to do it. It’s rather simple.
To get this working, we need to use the WooCommerce filter called woocommerce_product_single_add_to_cart_text. We’ll hook in to that and override the text that displays.
Replacing the WooCommerce “Add To Cart” text
Let’s dive straight in to the snippet.
<?php /** * Change the label from "Add to Cart" to "Download". * * @param string $text The existing label. * @return string */ function thenga_single_add_to_cart_text( $text ) { return __( 'Download', 'textdomain' ); } add_filter( 'woocommerce_product_single_add_to_cart_text', 'thenga_single_add_to_cart_text', 10, 1 );
In the example above, we’ve simply replaced the words “Add To Cart” with the word “Download”. You can change the text to whatever you would like. Also make sure you replace the textdomain with the correct one for your theme or plugin.
Changing the “Add To Cart” text based on product data
Now let’s look at a more advanced example. Let’s change the text based on whether the product is free or not. Here is the snippet.
<?php /** * Change the label from "Add to Cart" to "Download" if the product is free. * * @param string $text The existing label. * @param object $product The product object. * @return string */ function thenga_single_add_to_cart_text( $text, $product ) { if ( empty( $product->get_price() ) ) { return __( 'Download FREE', 'textdomain' ); } return $text; } add_filter( 'woocommerce_product_single_add_to_cart_text', 'thenga_single_add_to_cart_text', 10, 2 );</pre> <pre>
As you can see, we’re checking if the product’s price is empty (or free/$0) and if it is, we return the text “Download FREE”. If it’s not, we return the original text which is “Add To Cart”. The same rules as the first example apply and since you have access to the $product object now, you can use whatever product data or attributes you would like to manipulate the text on the “Add to Cart” button.
We really hope you found this snippet useful! Check out some of our WooCommerce products that you can use on your store to grow sales and build a better store.