WooCommerce: Edit “Add to Cart” Text by Product Category
Today we take a look at the WooCommerce “Add to Cart” buttons. What if you wanted to change the “Add to Cart” text depending on the Product Category? For example, you may want to show “Buy Now” for books and “Add to Basket” for cds.
PHP Snippet: Change “Add to Cart” Text by Product Category (2 Categories Only)
/**
* @snippet WooCommerce: Edit "Add to Cart" Text by Product Category
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 6
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_product_add_to_cart_text', 'bbloomer_archive_custom_cart_button_text' );
function bbloomer_archive_custom_cart_button_text() {
global $product;
if ( has_term( 'category1', 'product_cat', $product->get_id() ) ) {
return 'Category 1 Add Cart';
} else {
return 'Category 2 Buy Now';
}
}
PHP Snippet: Change “Add to Cart” Text by Product Category (Improved Snippet by Fabio Tielen)
/**
* @snippet WooCommerce: Edit "Add to Cart" Text by Product Category
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 6
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_product_add_to_cart_text', 'bbloomer_archive_custom_cart_button_text' );
function bbloomer_archive_custom_cart_button_text() {
global $product;
$terms = get_the_terms( $product->get_id(), 'product_cat' );
foreach ( $terms as $term ) {
$product_cat = $term->name;
break;
}
switch( $product_cat ) {
case 'category1';
return 'Category 1 button text'; break;
case 'category2';
return 'Category 2 button text'; break;
// case 'category3'; etc...
// return 'Category 3 button text'; break;
default;
return 'Default button text when no match found'; break;
}
}
This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101
Related content
WooCommerce: Custom Add to Cart URLs – The Ultimate Guide In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
WooCommerce: Hide Price & Add to Cart for Logged Out Users You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
WooCommerce: Remove / Edit “Added to Your Cart” Message A client asked me to completely remove the message that appears after you add a product to the cart from the product page. This is simply done by using a PHP snippet, so here’s the quick fix for you!
WooCommerce: Add to Cart Quantity Plus & Minus Buttons Here’s a quick snippet you can simply copy/paste or a mini-plugin you can install to show a “+” and a “-” on each side of the quantity number input on the WooCommerce single product page and Cart page. The custom code comes with a jQuery script as well, as we need to detect whether the […]
WooCommerce: Redirect to Checkout on Add to Cart Here’s how you force redirection to the checkout page every time a product is added to cart, no matter if you do so from the loop/shop pages or the single product page. On top of adding the code to your functions.php you also need to double check your WooCommerce settings, see screenshot below. Enjoy!
Rodolfo Melogli
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli
31 thoughts on “WooCommerce: Edit “Add to Cart” Text by Product Category”
Hi Drew, this works for default WooCommerce. You should give it another go without other plugins installed and on a custom theme, just to double check it works
Hello,
At this particular moment, the second snippet – Change βAdd to Cartβ Text by Product Category (Improved Snippet by Fabio Tielen) – produces a notice on my website. Here is the notice message:
Notice
: ID was called
incorrectly
. Product properties should not be accessed directly.
What is very curious is that I activate a plugin named WishList Member, the notice disappears.
The improved version doesnβt work for me β what happens is that when you add multiple categories, all of the add to cart buttons end up having the default value, which doesnβt work if you need at least one category to have a different button label. I was able to get it to work with this:
/**
* @snippet WooCommerce: Edit "Add to Cart" Text by Product Category
* @how-to Get CustomizeWoo.com FREE
* @sourcecode https://businessbloomer.com/?p=19944
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.7.0
*/
// Part 1
// Edit Single Product Page Add to Cart Button
add_filter( 'woocommerce_product_single_add_to_cart_text', 'bbloomer_custom_add_to_cart_single_product' );
function bbloomer_custom_add_to_cart_single_product() {
global $product;
if ( has_term( array('category-1', 'category-2'), 'product_cat', $product->ID ) ) {
return 'Category Button Text Here';
} else {
return 'Default Button Text Here';
}
}
// Part 2
// Edit Archive Pages Add to Cart Buttons
add_filter( 'woocommerce_product_add_to_cart_text', 'bbloomer_archive_custom_cart_button_text' );
function bbloomer_archive_custom_cart_button_text() {
global $product;
if ( has_term( array('category-1', 'category-2'), 'product_cat', $product->ID ) ) {
return 'Category Button Text Here';
} else {
return 'Default Button Text Here';
}
}
Chris, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?
To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.
Hi Rodolfo,
I have altered this snippet to work on Add to Cart buttons on Single Product pages within a certain category as well. I have simply added this snippet after the above snippet. If there is a way to combine these options within one snippet, that would be helpful – (sorry -new to PHP!)
/**
* @snippet WooCommerce: Edit "Add to Cart" Text by Product Category for Single Product
* @how-to Get CustomizeWoo.com FREE
* @sourcecode https://businessbloomer.com/?p=19944
* @author Michael & Fabio Tielen & Rodolfo Melogli
* @testedwith WooCommerce 3.4.3
*/
add_filter( 'woocommerce_product_single_add_to_cart_text', 'bbloomer_custom_add_cart_button_single_product' );
function bbloomer_custom_add_cart_button_single_product() {
global $product;
$terms = get_the_terms( $product->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
break;
}
switch($product_cat)
{
case 'category1';
return 'Category 1 button text'; break;
case 'category2';
return 'Category 1 button text'; break;
// case 'category3'; etc...
// return 'Category 3 button text'; break;
default;
return 'Default button text when no match found'; break;
}
}
Hi Rodolfo,
This worked great for Category archive pages, but I need snippet to change button text on the single product pages based on category as well. I took a look at the Conditional logic links, but couldn’t understand how to pass this advanced code into the right space. Do you have an example snippet for this situation?
Hi Rodolfo
thank you for sharing these useful information and codes, i have a question on this code.
does it work on product category name or slug?
can u do some changes on it for work with category slugs?
does it still work with woocommerce 3.3.3?
I thought it was doing nothing then realized it had changed the text of the button that appears when you hover over the image in your shop but not the main add to cart button.
Thank you! For the time being, due to time constraints (and as useful as it would be to learn all this) I have installed a free plugin to allow me to edit them without knowing any coding. As I am mostly just setting up my own site and don’t plan on making a living of it, I prefer to ask or pay someone who does, for their expertise. Which is why, if I keep coming here to pick your brain, I will most likely invest in a subscription for a year π
I am just starting with php programming and I found your video tutorial so encouraging! Thank you very much for all your work and sharing!
The snippet you are explaining here is exactly what I need – basically to change the add to cart button text by product category. and I’ve been struggling for days to achieve this !
I followed all your instructions but unfortunately it didn’t work π I am sure I am missing something.. maybe because I am working on woocommerce 2.6.14 or probably because I pasted the code at the wrong place… I really don’t …
anyway if could give me any help I would really appreciate. thanks!
Questions? Feedback? Customization? Leave your comment now! _____
If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!
This code made no change to our buttons.
Confirmed by our expert dev team.
Thanks for trying.
Hi Drew, this works for default WooCommerce. You should give it another go without other plugins installed and on a custom theme, just to double check it works
Hello,
At this particular moment, the second snippet – Change βAdd to Cartβ Text by Product Category (Improved Snippet by Fabio Tielen) – produces a notice on my website. Here is the notice message:
What is very curious is that I activate a plugin named WishList Member, the notice disappears.
Snippets fixed, thanks for pointing that out!
The improved snippet worked perfect for me!
Top
Hi, I tried the code but it didn’t work. I have a category page (overige producten): https://printservice-oosterhout.nl/product-categorie/overige-producten/ and I wand the button text to change from Add to cart in to: In winkelmand.
I tried many codes but I can’t get any of them to work. Can you help me out?
Kind regards,
Bernardine
Hi Bernardine, thanks so much for your comment! Yes, if you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
That is huge… The function worked for me very well as I wanted for my project.
Cool
The improved version doesnβt work for me β what happens is that when you add multiple categories, all of the add to cart buttons end up having the default value, which doesnβt work if you need at least one category to have a different button label. I was able to get it to work with this:
Nice, well done!
Hi Rodolfo,
I’m looking for a snippet that will allow me to change the ADD TO CART button on SOLD OUT items. Do you have a snippet you could share for that?
Much thanks for all your tips and snippets!
Patty
Maybe something similar to: https://businessbloomer.com/woocommerce-show-sold-archiveproduct-page/
Does not work
Chris, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?
To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.
Hope this helps!
R
Hi Rodolfo,
I have altered this snippet to work on Add to Cart buttons on Single Product pages within a certain category as well. I have simply added this snippet after the above snippet. If there is a way to combine these options within one snippet, that would be helpful – (sorry -new to PHP!)
Nice work Michael π
Hi Rodolfo,
This worked great for Category archive pages, but I need snippet to change button text on the single product pages based on category as well. I took a look at the Conditional logic links, but couldn’t understand how to pass this advanced code into the right space. Do you have an example snippet for this situation?
Thanks,
Michael
Hi Rodolfo
thank you for sharing these useful information and codes, i have a question on this code.
does it work on product category name or slug?
can u do some changes on it for work with category slugs?
does it still work with woocommerce 3.3.3?
Hey there, thanks for your comment! Yes, it works with latest Woo and you can customize it as you wish. It works with slugs π
I thought it was doing nothing then realized it had changed the text of the button that appears when you hover over the image in your shop but not the main add to cart button.
Hey Emily, thanks for your comment! I guess you’re using a custom theme, as this would work on a default WooCommerce theme π
The code unfortunately does not work anymore with WooCommerce v3
π
Hey JP, thanks so much for your comment! Which of the 2 snippets are you using?
Hello, Me again π
Do you have an existing snippet to do this for the buttons on the single/variable product pages?
π
You can take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/.
Let me know π
Thank you! For the time being, due to time constraints (and as useful as it would be to learn all this) I have installed a free plugin to allow me to edit them without knowing any coding. As I am mostly just setting up my own site and don’t plan on making a living of it, I prefer to ask or pay someone who does, for their expertise. Which is why, if I keep coming here to pick your brain, I will most likely invest in a subscription for a year π
Thank you Reese π
Dear Rodolf,
I am just starting with php programming and I found your video tutorial so encouraging! Thank you very much for all your work and sharing!
The snippet you are explaining here is exactly what I need – basically to change the add to cart button text by product category. and I’ve been struggling for days to achieve this !
I followed all your instructions but unfortunately it didn’t work π I am sure I am missing something.. maybe because I am working on woocommerce 2.6.14 or probably because I pasted the code at the wrong place… I really don’t …
anyway if could give me any help I would really appreciate. thanks!
Hey Valerio, thanks for your comment! Can you paste here the snippet you’ve used, and also tell me where you pasted the code? Thanks!