stevetronix
stevetronix

Reputation: 1261

Changing the Add To Cart button text in WooCommerce for items with variations

I'm running WooCommerce version 2.5.5. The following lines of code don't seem to change the Add To Cart button text on my product page for an item with variations:

add_filter('variable_add_to_cart_text', 'my_custom_cart_button_text');

function my_custom_cart_button_text() {

        return __('Buy Now', 'woocommerce');

}

Would you happen to know what I'm missing?

Upvotes: 2

Views: 6120

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Update: for WooCommerce 3+

You are using an obsolete hook for prior versions 2.1 of WooCommerce (see at the bottom the reference).

First you can target the desired product type in those (new) hooks with conditions:

global $product;

if ( $product->is_type( 'simple' ) ) // for simple product
    // Your text for simple product

if ($product->is_type( 'variable' ) ) // for variable product
    // Your text for variable product

if ($product->is_type( 'grouped' ) ) // for grouped product
    // Your text for grouped product

if ($product->is_type( 'external' ) ) // for external product
    // Your text for external product

Now you have 2 available hooks for Woocommerce:

  • One for single product pages (targeting the product types with conditions):
add_filter( 'woocommerce_product_single_add_to_cart_text', 'my_custom_cart_button_text', 10, 2 );
  • And the other for product archives pages (targeting the product types with conditions):
add_filter( 'woocommerce_product_add_to_cart_text', 'my_custom_cart_button_text', 10, 2 );

And you will use one or both of them with your customized function targeting through the variables product type condition, this way:

function my_custom_cart_button_text( $button_text, $product ) {

    if ( $product->is_type( 'variable' ) )
        $button_text = __('Buy Now', 'woocommerce');

    return $button_text
}

You can also have a custom button text by product type: See here.


Reference: Change add to cart button text

Upvotes: 5

helgatheviking
helgatheviking

Reputation: 26319

The correct filter for the single product page is woocommerce_product_single_add_to_cart_text.

function my_custom_cart_button_text( $text, $product ) {
    if( $product->is_type( 'variable' ) ){
        $text = __('Buy Now', 'woocommerce');
    }
    return $text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'my_custom_cart_button_text', 10, 2 );

Upvotes: 8

Related Questions