WooCommerce Add to Cart Text Generator
Change the WooCommerce add to cart button text on the product page, in the shop loop, or only for one product type or category.
<?php
/**
* Add to cart button wording.
*/
/**
* The button on the product page.
*
* @param string $text Current label.
* @param WC_Product $product The product being shown.
* @return string
*/
function my_store_single_add_to_cart_text( $text, $product ) {
if ( ! $product->is_in_stock() ) {
return $text;
}
return __( 'Buy now', 'my-store' );
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'my_store_single_add_to_cart_text', 10, 2 );
/**
* The button in the shop and category listings.
*
* @param string $text Current label.
* @param WC_Product $product The product being listed.
* @return string
*/
function my_store_loop_add_to_cart_text( $text, $product ) {
// A variable product links to the page instead of adding to the cart, so
// leave its wording alone unless you mean to promise something it cannot do.
if ( ! $product->is_purchasable() || ! $product->is_in_stock() ) {
return $text;
}
return __( 'Add to basket', 'my-store' );
}
add_filter( 'woocommerce_product_add_to_cart_text', 'my_store_loop_add_to_cart_text', 10, 2 );
Output is valid and updates as you type.
Fix the highlighted fields to update the output.
Change what the add to cart button says, on the product page, in the listings, or only for one product type or category. The generator writes both filters, because the two buttons are not the same one.
How to use
- Set the product page wording first. That is the button most people mean when they say “add to cart”.
- Leave the listing filter on. The shop grid uses a separate filter, which is why one button changes and the other does not.
- Narrow the scope when the wording only fits part of the catalogue. “Download now” on a physical product is worse than the default.
- Be careful in the listing: a variable product’s button links to the product page instead of adding anything, so the generated code leaves those alone.
- Put the snippet in a site specific plugin so the wording survives a theme change.
Example
Different wording for one category only, on both buttons:
function acme_single_add_to_cart_text( $text, $product ) {
if ( ! has_term( 'gift-cards', 'product_cat', $product->get_id() ) ) {
return $text;
}
if ( ! $product->is_in_stock() ) {
return $text;
}
return __( 'Send this gift', 'acme' );
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'acme_single_add_to_cart_text', 10, 2 );
Returning $text rather than your own string in every case you do not handle is what keeps the rest of the store untouched.
Pitfalls
- The product page and the listing use different filters. Changing one and wondering why the other is unchanged is the most common report.
- Both filters pass the product as the second argument, but only if you register with
10, 2. Leave the argument count off and$productis null. - A variable product in the listing shows a link, not an add to cart button. Labelling it “Add to cart” promises something the button does not do.
- Out of stock products keep their own label. Overriding it without checking stock tells people to buy something they cannot.
- Translating your new string with a text domain your snippet does not load leaves it untranslated. Use the plugin’s own domain.
- External products use the button text stored on the product. A filter here competes with what the merchant typed in the product editor.
- Wording changes affect analytics that key on button text. Check any event tracking before shipping.
- Caching plugins serve the old label from HTML cache. Clear it after deploying.
Compatibility
woocommerce_product_single_add_to_cart_text and woocommerce_product_add_to_cart_text have both passed the product object since WooCommerce 3.0, and still apply in WooCommerce 8 and 9 with classic product templates. Product blocks in a block theme render the same filters through the add to cart block. The generated code targets PHP 7.0 and up, and the tool runs entirely in your browser.
Frequently asked questions
Why did only one button change?
How do I change it for one product only?
if ( 123 !== $product->get_id() ) { return $text; }. Category or type scoping is usually easier to maintain.