WooCommerce : Change product’s add-to-cart button text

No code solutions

You can easily do this with these Free Plugins

Coding Solutions

To change product text, You’ll need to use two filter hooks

woocommerce_product_single_add_to_cart_text

This hook is defined in woocommerce/includes/class-wc-product-external.php:171 and used to filter out add_to_cart button text value for single page only.

	/**
	 * Get the add to cart button text for the single page.
	 *
	 * @access public
	 * @return string
	 */
	public function single_add_to_cart_text() {
		return apply_filters( 
		  'woocommerce_product_single_add_to_cart_text', 
		  $this->get_button_text() ? 
		      $this->get_button_text() :
		       _x( 'Buy product', 'placeholder', 'woocommerce' ), 
		  $this 
		);
	}
PHP

Have 2-parameters

  1. Actual Add to Cart Button text
  2. Product Object

To change add_to_cart across all type of products list, You’ll need to use this hook :

woocommerce_product_add_to_cart_text

This hook is also defined in the same file as the previous hook.

	/**
	 * Get the add to cart button text.
	 *
	 * @access public
	 * @return string
	 */
	public function add_to_cart_text() {
		return apply_filters( 
		  'woocommerce_product_add_to_cart_text', 
		  $this->get_button_text() ? 
		    $this->get_button_text() : 
		    _x( 'Buy product', 'placeholder', 'woocommerce' ), 
		  $this 
		);
	}
PHP

also contains 2 parameters like the last hook.

Let’s see some examples.

All Products
<?php

// Change add to cart text on single product page
add_filter( 
  'woocommerce_product_single_add_to_cart_text', 
  'change_button_text_single' 
); 
function change_button_text_single($text) {
    return __( 'Buy Now', 'text-domain' ); 
}

// Change add to cart text on product list page
add_filter( 
  'woocommerce_product_add_to_cart_text', 
  'change_button_text_lists' 
);  
function change_button_text_lists($text) {
    return __( 'Buy Now', 'text-dmain' );
}
PHP

Only Simple Products
<?php

// Change add to cart text on single product page
add_filter( 
'woocommerce_product_single_add_to_cart_text', 
'change_button_text_single', 10, 2 ); 
function change_button_text_single($text, $product) {
    if ( "simple" === $product->get_type() ) {
      return __( 'Buy Now', 'text-domain' );
    } 
    
    return $text;
}

// Change add to cart text on product list page
add_filter( 
  'woocommerce_product_add_to_cart_text', 
  'change_button_text_lists', 10, 2 );  
function change_button_text_lists($text, $product) {
    if ( "simple" === $product->get_type() ) {
      return __( 'Buy Now', 'text-domain' );
    } 
    
    return $text;
}
PHP

Only Digital products

First, We need to check if the product is virtual. If the product is virtual then just return the Text else return the default text.

// Change add to cart text on single product page
add_filter(
  'woocommerce_product_single_add_to_cart_text', 
  'change_button_text_single', 10, 2);
function change_button_text_single($text, $product)
{
    // check if product is virtual
    if (true === $product->get_meta('_virtual')) {
        return __('Get Now', 'text-domain');
    }

    return $text;
}

// Change add to cart text on product list page
add_filter(
  'woocommerce_product_add_to_cart_text', 
  'change_button_text_lists', 10, 2);
function change_button_text_lists($text, $product)
{
    // check if product is virtual
    if (true === $product->get_meta('_virtual')) {
        return __('Get Now', 'text-domain');
    }

    return $text;
}
PHP

Thanks.

Istiaq Nirab

A highly skilled and proficient WordPress developer with over Three years of professional experience in all aspects of WordPress website creation, including design, plug-ins, and implementation. Results-driven individual contributor with a successful track record in exceeding expectations for web development initiatives

More Reading

Post navigation

Leave a Comment

Leave a Reply

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