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 businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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 businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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 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!