There’s an easy way to replace the default WooCommerce Buy product button text on the External / Affiliate products by editing the functions.php file of your WordPress theme. But, here I show how to change the button text using MySQL.
The MySQL method is helpful if you want to keep the ability to have different button texts on different external / affiliate products, since the method via functions.php forces the text on all of them to change to the single text you choose.
So, here’s the MySQL command to replace text in the products’ _button_text field (in bulk):
UPDATE wp_postmeta SET meta_value = replace(meta_value,'OLD TEXT','NEW TEXT') WHERE meta_key = '_button_text';
The MySQL method works if you have already entered a custom button text instead of the default Buy product text in the Button text field of your External / Affiliate products. This is normally done in the Product data section of the regular WooCommerce product editor in the wp-admin area of your website.
If you haven’t changed the default Buy product text in the Button text field, the particular product won’t have its _button_text field in the wp_postmeta MySQL table.
Nevertheless, if you don’t mind having a fixed single variant of the button text here’s the functions.php code snippet for changing the default Buy product button text to your desired text:
// Change Buy product text on product archives pages
add_filter('woocommerce_product_add_to_cart_text', 'woocommerce_add_to_cart_button_text', 10, 2);
// Change add to cart text on single product page
add_filter('woocommerce_product_single_add_to_cart_text', 'woocommerce_add_to_cart_button_text', 10, 2);
function woocommerce_add_to_cart_button_text($button_text, $product) {
if ('external' == $product->get_type()) {
$button_text = __('New External Link Button Text', 'woocommerce');
}
return $button_text;
}
If you want me to implement this on your WooCommerce installation, feel free to contact me.
Leave a Reply