Change the add to cart button text in WooCommerce 2.1+
A many asked question is how to change the add to cart button text in WooCommerce 2.1 and above. In our WooCommerce Uploads Before Add-on we use the function below to change the text into “Add to cart & Upload Files”, but what if you want to change it into something else?
Change the add to cart text on the product page
It’s easy to change the add to cart text on the product page with a default WooCommerce filter. To change it, add the following code to your theme’s functions.php or in a custom plugin.
add_filter( 'woocommerce_product_single_add_to_cart_text', 'wpf_custom_add_cart_text',11);
function wpf_custom_add_cart_text() {
return __( 'Buy this product!', 'woocommerce' );
}
If you change the priority (in this example #11 at the end of the filter) you can “override” the text if the text is already changed inside a plugin (like ours) or inside your theme.
Change the add to cart text on the overview pages
If you want to change the add to cart text on the overview pages (archives) you can use the following code inside your theme’s functions.php or in a custom plugin.
add_filter( 'woocommerce_product_add_to_cart_text' , 'wpf_custom_add_cart_text_archive',11);
function wpf_custom_add_cart_text_archive() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'external':
return __( 'Buy product', 'woocommerce' );
break;
case 'grouped':
return __( 'View products', 'woocommerce' );
break;
case 'simple':
return __( 'Add to cart', 'woocommerce' );
break;
case 'variable':
return __( 'Select options', 'woocommerce' );
break;
default:
return __( 'Read more', 'woocommerce' );
}
}
We hope these two small code snippets will make your WooCommerce site stand out of all the other 😉
Contact us if you’ve any questions or leave your comment below.