How to Change the WooCommerce “Add To Cart” text

Do you need to change the text that displays on the WooCommerce “Add To Cart” button? We needed to recently and in this snippet, this article we will show you exactly how to do it. its very simple.

To get this working, we need to use the WooCommerce filter and its called woocommerce_product_single_add_to_cart_text. We will hook in to that and override the text that displays.

Go to Your theme file wp-content->themes-> themename-> functions.php file bottom area added this php code

/**
 * Change the label from "Add to Cart" to "Download" if the product is free.
 * @param string $text The existing label.
 * @return string
 */
function themepiko_single_add_to_cart_text( $text ) {
    return esc_html__( 'Download', 'themepiko' );
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'themepiko_single_add_to_cart_text', 10, 1 );

Changing the “Add To Cart” text based on product price zero

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 if product price zero then show Download now Text

/**
 * Change the label from "Add to Cart" to "Download Now" if the product is free.
 * @param string $text The existing label.
 * @param object $product The product object.
 * @return string
 */
function themepiko_single_add_to_cart_text( $text, $product ) {
   if ( empty( $product->get_price() ) ) {
      return esc_html__( 'Download Now', 'themepiko' );
   }
 
   return $text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'themepiko_single_add_to_cart_text', 10, 2 );

Changing the “0.00” Price text based on product price Free

In older versions of WooCommerce free prices used to display as “FREE!” and products with empty prices were not publishable/purchasable. Now they’ve changed this around, but I still believe “FREE” looks much better than “$0.00”. It’s much more enticing, isn’t it?

Well, here’s how you restore the old WooCommerce functionality as usual it’s as simple as using a PHP filter provided by WooCommerce and overriding the default behavior.


/**
 * Change the label from "0.00" to "Free" if the product is 0.00.
 * @param string $price The existing label.
 * @return string
 */
 function themepiko_free_product($price)
        {
            $clear = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($price))))));
            if ($clear == "0 00") {
                return esc_html__('Free', 'themepiko');  
            }
            return $price;
        }
add_filter('woocommerce_get_price_html', 'themepiko_free_product');
add_filter('woocommerce_cart_item_price', 'themepiko_free_product');

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.

Leave a Reply

MENU