Istiaq Nirab full stack + ai
← Writing
WooCommerce Sep 22, 2023

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

Change the add-to-cart button text of WooCommerce products with plugins or with two filter hooks.

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 
		);
	}

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 
		);
	}

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' );
}

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;
}

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;
}

Thanks.

Discussion

Comments run on giscus, backed by GitHub Discussions — sign in with GitHub to reply.

More posts
Laravel

Setup vsCode for Laravel Development

The extensions and keyboard shortcuts that turn Visual Studio Code into a proper Laravel editor.

2023-09-22
WooCommerce

WooCommerce apply coupon via url

Apply woocommerce coupon via custom url

2023-03-19
vsCode

vsCode Configuration for WordPress Plugin

Configure vscode to experience better on develop WordPress Plugin.

2022-12-17
ContactGMT+6

Have something that needs building, fixing, or finishing? Write one paragraph — that's usually enough.

open to new work
Local time--:--:--
Based inDhaka, BD
Experience6+ years