WooCommerce labels its buttons “Add to cart”. This snippet changes the WooCommerce Add to cart button text to your own wording, for example “Buy now”, on the single product page and in the shop lists, each with its own text.
In shop lists it only renames buttons that really add the product to the cart. “Select options” on variable products and “Read more” on out of stock products keep their text, so the button never promises something it does not do.
The snippet
<?php
/**
* Change the WooCommerce "Add to cart" button text on product pages and in product lists.
*/
// ===== Config =====
function dpc_add_to_cart_text_config() {
$config = array(
// Button on the single product page. Empty '' = keep the WooCommerce text.
'single' => __( 'Buy now', 'dpc' ),
// Button in the shop, category pages, related products and product shortcodes. Empty '' = keep.
'archive' => __( 'Buy now', 'dpc' ),
// false = in lists, change only buttons that put the product straight into the cart;
// "Select options", "View products" and "Read more" (out of stock) stay as they are.
// true = change every button in lists, whatever it does.
'archive_all' => false,
);
// ===== End of config =====
return (array) apply_filters( 'dpc_add_to_cart_text_config', $config );
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'dpc_single_add_to_cart_text' );
function dpc_single_add_to_cart_text( $text ) {
$config = dpc_add_to_cart_text_config();
return '' !== (string) ( $config['single'] ?? '' ) ? (string) $config['single'] : $text;
}
add_filter( 'woocommerce_product_add_to_cart_text', 'dpc_archive_add_to_cart_text', 10, 2 );
function dpc_archive_add_to_cart_text( $text, $product = null ) {
$config = dpc_add_to_cart_text_config();
if ( '' === (string) ( $config['archive'] ?? '' ) ) {
return $text;
}
if ( empty( $config['archive_all'] ) ) {
$adds_to_cart = $product instanceof WC_Product
&& $product->supports( 'ajax_add_to_cart' )
&& $product->is_purchasable()
&& $product->is_in_stock();
if ( ! $adds_to_cart ) {
return $text;
}
}
return (string) $config['archive'];
}
How it works
woocommerce_product_single_add_to_cart_textsets the button on the single product page, for every product type.woocommerce_product_add_to_cart_textsets the button in lists: the shop page, category pages, related products, upsells and product shortcodes.- In lists the snippet checks that the product supports the direct add to cart button, can be bought and is in stock, which is the case for a simple product in stock. Other buttons keep WooCommerce’s text unless you set
archive_alltotrue. - The texts are translatable with the
dpctext domain, notwoocommerce, so WooCommerce’s own translations cannot overwrite them. WooCommerce escapes the text when it prints the button. - All function names start with
dpc_. Thewoocommerce_prefix used in older copies belongs to WooCommerce and can collide with its functions or other snippets.
Add it with Scripts Organizer
Scripts Organizer ships this snippet in its One-Click Import library as Change Add to Cart button text. To add it by hand, go to Code Blocks → Add New and give the block a name. Set Trigger location to Everywhere, tick PHP under Script location, paste the code into the PHP editor, edit the config and publish. Leave Action/Hook empty.
The code only adds filters, so it can load before WooCommerce. Product lists are also rendered in REST requests and blocks, which Everywhere covers.
The same code also works unchanged in a small plugin or an mu-plugin.
Customise it
- Different wording per place: change
singleandarchive. An empty string keeps WooCommerce’s text there. - Rename every list button, including “Select options” and “Read more”: set
'archive_all' => true. - Show another text when the product is already in the cart: see Woo: Rename “add to cart” button if product already in cart.
- Change the out of stock label as well: see Woo : Change “Out of Stock” to SOLD OUT.
Gotchas
- The button still adds the product to the cart. “Buy now” does not skip the cart and go straight to checkout.
- Block-based shops using the Product Collection block show the same list text. The single product Add to Cart with Options block uses the single text.
- External/affiliate products use the button text set on each product, unless
archive_allis on.
The older version of this post changed every button to “Buy Now”, also on variable and out of stock products, used function names with WooCommerce’s own prefix and the woocommerce text domain.
In the Scripts Organizer library Import it with one click from Scripts Organizer.