How to Change the “Buy product” Button Text in Woocommerce Sitewide

How to Change the “Buy product” Button Text in Woocommerce Sitewide

By default, the Buy Button text for external/affiliate products shows “Buy product” on Woocommerce product pages and on search/archive pages, which is kind of dull and drab.  And if your Woocommerce store is strictly all external products (meaning the buy button goes to an affiliate vendor), then you might want to change that programmatically.  This article will show you how. It’s very easy top do.

While Wocommerce does allow you the ability to change the text of the external/affiliate products buy button right in the product’s edit page in the back-end of WordPress, that isn’t ideal for sites that have a lot of products and sometimes you just don’t want to deal with making sure you fill-in yet another field when creating a new product page.

The solution is to add a filter to child theme’s functions.php file.  It’s really as simple as that.  Here’s the code you need to add to the bottom of your functions.php file…

/////////////////////////////////////////////////////////////////////
// Change "Buy product" button text site-wide for external products
/////////////////////////////////////////////////////////////////////
function change_wc_external_button( $button_text, $product ) {

    if ( 'external' === $product->get_type() ) {
        // enter the default text for external products
        return $product->button_text ? $product->button_text : 'Details';
    }

    return $button_text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'change_wc_external_button', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'change_wc_external_button', 10, 2 );

The above filter code changes the buy button text to “Details”, but you can change it to whatever text you want. So, for instance, if all your external/affiliate  products in your wocommerce site are products you promote for, say, Ebay, then you can replace the word “Details” in the above code with something like “Buy on Ebay, or something similar.

Now, just so you know, when you go to add a new external product or edit an existing one, the button text field placeholder text will still show “Buy product”, but as long as you leave that field blank, and you have this filter in your functions.php file, the button for all your affiliate/external products will show as whatever you set the text to be in the filter code above, site-wide.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.