Change the “Add to Cart” text on the single product page, conditionally

For any reason, you could change the “Add to Cart” text in your single product page in WooCommerce.

Maybe you are based in UK, so it would be “Add to Basket” and you don’t want to use the plugin translation, or simply you want it to say something cool and unique.

There’s a way to change the text on all products with a filter.

Open the file functions.php located in wp-content/themes/your-theme-name/ and add this code at the end of the file:

This code will change the button text on all your products. What if you want to change it conditionally, in example, based on the product type?

There’s a little change you need in the previous code. It should be like this:

In this code we check the product type and return a different text based on that.
You can obviously add more types if you want to. For example if you use WooCommerce Photography you could add the type photography.


More Posts That You Might Like…


29 responses to “Change the “Add to Cart” text on the single product page, conditionally”

  1. Thanks for your post,
    I’m trying to change the other text label of the botton “check out” but I don’t know how using your code with other bottons 🙁

    1. Hi Michele,
      Add this code in the file functions.php located in your theme folder in wp-content/themes/your-theme-name/.

      https://gist.github.com/a857302a4654cf4aee6e

      If you want, you can also edit the file directly from the dashboard in WordPress. To do so, navigate to Appearance > Editor and find the file Theme Functions (functions.php) in the list on the right of the page, like in this screenshot: https://cld.wthms.co/12U9T

      If you file ends with the sign ?> be sure to add the code one line before this sign.

      Be sure to chang ethe text “Proceed to Checkout” to what you need.

  2. Hello there,

    what if I would like to change the text of the “Update Cart” button in the cart page ?

    Thanks for your help,

    Roberto

    1. Hi Roberto,
      to change that you have to replace the template cart.php in your theme and change the button text on line 128.

      To learn how to override templates in WooCommerce follow this tutorial: https://docs.woothemes.com/document/template-structure/

  3. Thanks for this snippet. What if I wanted to change the text conditionally by Product ID, rather than type? How could I provide the conditions for say, 3 specific product IDs with a default “add to cart message” covering everything else?

    1. Hi Grant,
      to change the text based on the product ID you can use this snippet: https://gist.github.com/8a200b55f6b785b4b4e8

      Replace 1, 2, 3 with the IDs of your products.

      1. Nicola,

        Thanks for that snippet.  That worked perfectly – what a fast response too!

        — 

        1. You’re welcome!

  4. Hi Nicola,

    This post is the closest solution that i found for my problem. My problem is how can i change the Add to cart text and its URL only for CERTAIN product?

    Not product type, but certain product. I think this is possible since each product has different product ID.

    1. Hi,
      instead of switch ( $product->product_type ) use switch ( $product->id ) and in the cases below use the product ID you want to check for!

      1. Hi there,

        I’m only changing one of my variable products to have a button that says “Donate”. I used the above conditional code with the change you suggest here and it has done nothing to the button on that product’s page.

        I only include case ‘productid’ : $text = ‘Donate’; break; and then left the default.

        Did I do something wrong?

        1. Hi Kristyn,

          Like I said in my comment above, you need to change the switch line to use $product->id instead of $product->product_type.

          Then in the case, use the product ID, like case 123:, where 123 is the product ID.

  5. Hi there

    Can anybody help me, tell me how to remove SINGLE PRODUCT PAGES in WooCommerce?

    I want by clicking on the picture of a product from the catalog, the right to open direct affiliate link, both of this page.

    https://bit.ly/1YMLmJL

    Best Regards.

  6. This is great! Next I need to be able to create a way to change the button text only if the user has a subscription. Any idea where I can find that.

    1. Hi Amanda,
      You can use the method user_has_subscription().

      Something like:

      if ( WC_Subscriptions_Manager::user_has_subscription( 123, 456, 'active' ) ) {
          // Your code here
      }
      

      Where 123 is the user ID, 456 is the subscription product ID, and 'active' is the status of the subscription.

  7. Hi Nicola,
    i need to change the text conditionally by product category.
    i’ve try in this way but don’t work:

    function wc_custom_single_addtocart_text( $text, $taxonomy ) {
        switch ( $taxonomy->product_cat ) {
            case 'Red'  : $text = 'Prenotazione'; break;
            case 'Regalo': $text = 'Acquista Regalo'; break;
            default        : $text = 'Acquista'; break;
        }
    
        return $text;
    }
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'wc_custom_single_addtocart_text', 10, 2 );
    
    1. Hi Daniele,
      Your code can’t work because the second function argument is not really $taxonomy. You added that, but the filter passes the product object as argument, not the taxonomy.

      Change $taxonomy to $product. Then inside the function, use $product_cats = wp_get_post_terms( $product->id, 'product_cat' ); to get the product’s categories. It will be an array.

      After that you can check if the array contains a specific category, and return a different text accordingly.

      1. I’ve change code in this way, but doesn’t work:

        function wc_custom_single_addtocart_text( $text, $product ) {
        $product_cats = wp_get_post_terms( $product->id, 'product_cat' );
            switch ( $product->product_cat ) {
                case 'RISTORANTI'  : $text = 'Utilizza il codice del tuo regalo'; break;
                case 'REGALI': $text = 'Acquista un Regalo'; break;
                default        : $text = 'Acquista'; break;
            }
        
            return $text;
        
        }
        add_filter( 'woocommerce_product_single_add_to_cart_text', 'wc_custom_single_addtocart_text', 10, 2 );
        
        1. That won’t work Daniele.
          As I told you in my previous message, wp_get_post_terms returns an array, you can’t just switch it.

          You have to loop it, for each element, check if it’s the category you need, and in that case, break the switch and exit the loop.

          Please look at the function documentation to understand more: https://codex.wordpress.org/Function_Reference/wp_get_post_terms

          You’ll find some usage examples.

  8. the code does not work either in this way, using the id category 🙁

    add_filter( 'woocommerce_product_single_add_to_cart_text', 'funzione_cat_add_to_cart_text' );
    function funzione_cat_add_to_cart_text( $default ) {
        global $post;
        $terms = get_the_terms( $post->ID, 'product_cat' );
        if ( array_key_exists( 10, $terms ) ) {
                $default = 'UTILIZZA IL CODICE GIFT CARD';
        } else {
            return $default;
        }
    }
    
  9. Hey! I just had the same question (how to change add to cart text based on category) and found some snippets that use the cat ID but it didn’t work. Your example above worked for me and I wanted to say THANKS! So, THANKS!

    1. You’re welcome Pam! 🙂

  10. Hi Nicola,

    I checked the github code you share but i’m wondering what figure to replace. Is it possible to replace the text in two parent categories. Will it apply to the children categories? Been stuck with this problem for quite a while.

    I appreciate any help. Thanks!

    1. Hi Stephen,
      Where you see this code:

      if ( $term->name == 'Posters' ) {
      

      You should change it by adding all the categories you want to change:

      if ( $term->name == 'Posters' || $term->name == 'Something' ) {
      

      This will change the text for the categories named Posters and Something. It does not apply to children categories automatically.

  11. Is there a way to change what the add to cart button does- only on certain item? Say if I want it to go to a form page?

  12. Thanks this code working for me

  13. Thanks for the lovely post. A great help, hope you continue to do this more often. I really like studying your posts. Cheers!

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: