Posted on

WooCommerce: Edit “Add to Cart” Text by Product Category

Hello WooCommerce Customizers!

Today is episode #12ย of my “18 snippets in 18 days” challenge. With 6 days to go, I hope you’ve enjoyed the snippets so far and that your WooCommerce code snippet library has been growing steadily ๐Ÿ™‚

I ask you to kindly share on social media and leave blog comments to support my marathon, a lot of work is going into this and feel free to share some love ๐Ÿ™‚

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.

WooCommerce: Change Add to Cart Text depending on the Category
WooCommerce: Change Add to Cart Text depending on the Category

WooCommerce Snippet: Change “Add to Cart” Text by Product Category (2 Categories Only)


/**
 * @snippet       WooCommerce: Edit "Add to Cart" Text by Product Category
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=19944
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.3.3
 */

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->ID ) ) {           
return 'Category 1 Add Cart';
} else {
return 'Category 2 Buy Now';
}
}

WooCommerce 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        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=19944
 * @author        Fabio Tielen & Rodolfo Melogli
 * @testedwith    WooCommerce 3.3.3
 */

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->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;
}
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

15 thoughts on “WooCommerce: Edit “Add to Cart” Text by Product Category

  1. 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        Watch tutorial @ https://businessbloomer.com/?p=19055
     * @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;
    }
    }
    
    1. Nice work Michael ๐Ÿ™‚

  2. 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

  3. 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?

    1. Hey there, thanks for your comment! Yes, it works with latest Woo and you can customize it as you wish. It works with slugs ๐Ÿ™‚

  4. 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.

    1. Hey Emily, thanks for your comment! I guess you’re using a custom theme, as this would work on a default WooCommerce theme ๐Ÿ™‚

  5. The code unfortunately does not work anymore with WooCommerce v3

    ๐Ÿ™

    1. Hey JP, thanks so much for your comment! Which of the 2 snippets are you using?

  6. Hello, Me again ๐Ÿ˜‰

    Do you have an existing snippet to do this for the buttons on the single/variable product pages?

    1. 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 ๐Ÿ˜‰

    2. Thank you Reese ๐Ÿ™‚

  7. 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!

    1. 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!

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.