WooCommerce: Change “Add to cart” Text Per Product Type
If you want to customize the “Add to cart” button text in WooCommerce based on the product type, this guide will show you how to do it quickly and efficiently. Changing the button text per product type can improve user experience by providing context-specific calls to action, such as “Select options” for variable products or “Read more” for external products.
Quick Fix
- Identify the product types you want to customize (simple, variable, grouped, external).
- Add a custom function to your theme’s
functions.php
or a site-specific plugin. - Use WooCommerce filters to change the button text based on product type.
- Test the changes on the frontend to confirm the new button text appears correctly.
Why This Happens
WooCommerce uses default button texts for different product types to guide customers appropriately. For example, variable products show “Select options” because customers need to choose variations before adding to cart. However, these defaults might not fit your store’s branding or messaging strategy. WooCommerce provides hooks and filters that allow developers to modify these texts without changing core files, ensuring updates won’t overwrite your customizations.
Requirements
- WooCommerce installed and active on your WordPress site.
- Access to your theme’s
functions.php
file or a custom plugin for adding PHP code. - Basic understanding of PHP and WordPress hooks.
- Optional: Child theme to avoid losing changes on theme updates.
Step-by-step
- Backup your site: Always create a backup before editing theme files.
- Open your theme’s
functions.php
file: Use FTP, cPanel file manager, or the WordPress theme editor. - Add the following code snippet:
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text', 10, 2 );
function custom_add_to_cart_text( $text, $product ) {
if ( ! is_a( $product, 'WC_Product' ) ) {
return $text;
}
switch ( $product->get_type() ) {
case 'simple':
$text = __( 'Buy Now', 'your-text-domain' );
break;
case 'variable':
$text = __( 'Choose Options', 'your-text-domain' );
break;
case 'grouped':
$text = __( 'View Products', 'your-text-domain' );
break;
case 'external':
$text = __( 'Visit Website', 'your-text-domain' );
break;
default:
$text = __( 'Add to cart', 'your-text-domain' );
break;
}
return $text;
}
- Save the file and refresh your WooCommerce shop and product pages. You should see the new button texts based on product types.
Common Pitfalls
- Editing the wrong file: Always use a child theme or a custom plugin to avoid losing changes after updates.
- Missing product type check: Ensure the product object is valid before calling methods to avoid PHP errors.
- Not using translation functions: Use
__()
or_e()
for strings to support localization. - Cache issues: Clear your site and browser cache if changes don’t appear immediately.
- Conflicts with other plugins: Some plugins may override button texts; test with plugins disabled if needed.
Works on
This method works on any WooCommerce installation regardless of your web server or control panel:
- Web servers: Apache, Nginx, LiteSpeed
- Control panels: cPanel, Plesk, DirectAdmin
- Hosting environments: Shared hosting, VPS, Dedicated servers
- Compatible with most themes and WooCommerce versions (tested on WooCommerce 7.x and later)
FAQ
- Q: Can I change the button text only on the shop/archive pages?
- A: Yes. Use the
woocommerce_product_add_to_cart_text
filter for archive/shop pages andwoocommerce_product_single_add_to_cart_text
for single product pages. The example code above uses both. - Q: How do I translate the new button texts?
- A: Wrap your strings with translation functions like
__()
and load your text domain properly in your theme or plugin. Use tools like Loco Translate to manage translations. - Q: What if I want different texts for specific products, not just product types?
- A: You can extend the function to check product IDs or categories and return custom texts accordingly.
- Q: Will this affect the functionality of the add to cart button?
- No. This only changes the button text, not the underlying functionality.
- Q: Can I use this code in a plugin instead of
functions.php
? - Yes. Creating a site-specific plugin is a best practice to keep your customizations independent of theme updates.