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
);
}
PHPHave 2-parameters
- Actual Add to Cart Button text
- 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
);
}
PHPalso 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' );
}
PHPOnly 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;
}
PHPOnly 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;
}
PHPThanks.
Leave a Comment