How to change the price string in WooCommerce Subscriptions

If you are selling subscriptions with WooCommerce, you are probably using WooCommerce Subscriptions to do so. And if you are using it, you already know the structure of the price string for subscription products.

It’s really hard to create a valid price string when you are selling subscriptions. There are a lot of factors to consider, like free trials, intervals, etc.
The developers handle this properly, but sometimes, it’s not correct for what you sell or your store workflow.

You can change the price string, but it could be more complex than it seems.

In this article we’ll cover three code samples to change the price string in three different cases.

How does it work?

We’ll use the PHP function str_replace to handle the changes. We will use arrays of product IDs when necessary to change the string only on some products.

All the snippets need to be added in the file functions.php located in wp-content/themes/your-theme-name/, at the end of the file.

The example is on a product that is renewed every 3 months, without a free trial or sign-up fee.

Change the string globally

In the first example we’ll change the string for all the subscription products in the store. The code to do so is:

The two filters used will change the price string everywhere, both in the product single page and the cart page.

Change the string only on some products

This example will change the price string only on the product single page and only on some products:

Handle complex strings

In some cases, in example if you have a free trial or/and a sign-up fee, the price string will be more complex.
If this is your case, you can copy/paste the line where we use str_replace and change the words to change the other parts of the string, like in this example:


More Posts That You Might Like…


40 responses to “How to change the price string in WooCommerce Subscriptions”

  1. The above website will go in and out of availability (shopping cart wise) at different points in the year.

    The problem I am encountering is with WooCommerce AJAX reloads on the check out page.

    Everywhere else in the site my overload for woocommerce_subscription_price_string works beautifully, however on the check out page it works(yay!) but is then overridden when the checkout page ajax fires and I end up with the “normal” woocommerce_subscription_price_string result.

    Any thoughts on how to overcome this? (I have currently disabled the ajax on checkout and things are ‘working’ but not happy)

    Thanks!

    1. Hi Donald,
      it looks like a cache issue. The cart is saved in transients to speed up the thing.

      The error could be from your theme or simply from the old cache. Try to delete the transients in WooCommerce > System Status > Tools and/or try to switch your theme to something else.

      Have a great week!

  2. Thank you Nicola, this is exactly what I’ve been looking for! I agree with Donald that it works beautifully, but does not seem to be working in my cart either. I’ve cleared my cache and transients, even switched themes to no avail. Any other thoughts?

    Thanks again, great snippet!

    1. Can you send me your code?
      Please don’t write it directly in your comment. Save it online somewhere and send me its URL. You could use this: https://gist.github.com/

  3. Nicola,

    Thank you for the post. Saved me hours. Works great.

    Eric

    1. You’re welcome Eric 🙂

  4. Whats the best way to change the order of the price string. For example I want to put the Sign-up fee before the monthly rate.

    Thanks!

    1. You can play with sprintf or just rebuild completely the string manually!

  5. We have annual subscriptions and are planning on increasing our rate for 2016. By increasing the product, new subscriptions will be increased but not old ones correct? We need to hire someone to assist us if there are any solutions, can you advise on developers?

    1. Hi!
      I would recommend contacting one of the following services:

    2. Did you ever get an answer to this? We are facing the same issue. We are updating our prices for recurring annual subscriptions and nervous about the old ones and if it will automatically update to new price or renew at the old price. Any advice would help!

  6. Hey
    I would be interested to know if this code still works / apply’s to current versions of WooCommerce? It’s exactly what I have needed and I have scoured the internet for a similar thing and came up empty.

    I have implemented it as stated above however it doesn’t seem to take any effect.

    Any help anyone can offer would be much appreciated! Been pulling my hair out over this for months!

    Thanks so much!

    1. Hi Courtney,
      Yes, it’s still up to date and should work.

      Please check your code and make sure that it is correct. I’d also try with a different theme to make sure that nothing is conflicting with it.

      1. Hey Nicola!

        Thanks so much for getting back to me 🙂

        I have checked the theme however it still doesn’t seem to be working. Is there anything else obvious that I might be missing?

        Thanks so much for all your help – much appreciated!

        1. No, can you post the code you are using on gist.github.com and send me the link so I can check if anything is wrong?

  7. Hey Nicola

    I finally got it to work. I didn’t realise it was for payments on every 3rd month. How do I set it for payments that are every month?

    Also is there a way to change the “free trial” text?

    Thanks

    1. Hi Courtney,
      On line 3 I use the every 3 months string as first argument for str_replace. You simply need to change that with what you want to change.

      Same for the free trial text.

  8. How about targeting the subscription price string, but not the total subscription price string? My customers are able to order multiples of a product in 2,3, and 6 month intervals so it’s confusing for them to see the following in the cart:

    Price: $52.80 every 2 months

    Total: $211.40 every 2 months

  9. The code you provided works great on the product pages, but what if you want to change it in the cart and on the checkout pages?

  10. Hi Nicola

    Finally got round to trying it (you shared the link to here from my WooCommerce ticket), and I’ve managed to make it work pretty much first time – thanks!

    I can now change (some of) the string text, and it now reads:

    “From: $300.00 per month for 2 months with a $395.00 initial payment.”

    I would ideally like it to read “Initial payment of $395, followed by two monthly payments of $300.”

    Is this possible?

    Many thanks, Simon.

    1. Hi Simon, I had the same issue and fixed it by following this post on stack overflow: http://stackoverflow.com/a/31460756

      I was also lead here by a support ticket for this very issue.

  11. Hi! Thank you so much for this. It helped me to edit mine as well.

    Do you have advice for how to edit the term ‘Trial End Date’ in the account/product profile page? See my screenshot, to see what I mean: https://staging.healclinics.com/wp-content/uploads/2017/06/Untitled.png

  12. Its great article hook working fine . 🙂

  13. Its great article. hook working fine . 🙂

    1. Awesome! I’m happy it works for you!

  14. Hi,

    What if i just want to change to just the price itself?

    From:
    $0.00 for 12 months and a $120.00 sign-up fee

    To:
    $120

    How can i change that? Thanks!

    1. Hey John,
      You could use a regular expression to only extract the price from the string.

      You can learn more about regular expressions here: https://www.tutorialspoint.com/php/php_regular_expression.htm

  15. What if you have two different types of subscriptions? One where it is just the subscription itself, and one where there are a console cost and a 1-month free trial along with the subscription. Originally I used a filter which I am attaching but it changes the format of all the subscriptions strings on the site and it doesn’t make sense if I have a subscription option that’s only the subscription itself. All of the other subscriptions can use that filter but the only thing that should show for that one is “$19.99/month.”

    https://gist.github.com/melmacpacattack/cf4130b7c7e45e042fecd1f707c768dd

    1. Hi Melvin,
      There’s not a specific hook for single products, so you need to check by yourself what product you’re working on.

      You can try to add global $product; at the beginning of the snippet and that should give you access to the current product so you can check what its ID is and eventually change the string.

      1. Melvin Reynolds Avatar

        Maybe this is something obvious and I’m drawing a blank but when you are creating an array to only access specific products, how do you find the product id to use in the array parameters. I see in the above example that they use (45, 90, 238). How would you find those product numbers to use in your own coding?

        1. To find the Product ID, go to the Products screen, hover over the product and the ID appears as shown here: http://d.wedj.at/Lsagqv

  16. Is it possible to only show the max price, we are using the subscriptions to sell jewelery. Customers can choose to buy it in 3 times or in 1 time. The 1 time is the max price as an variable set for 1 month subsription, the 2 times is set for 2 month, the allready pay the first month with an ideal payment, the next to months go by subscription. We like to see the max price, because on our facebook we use an advertisement program but is only displays the min variable price.

    1. Hi Edwin,
      The price string is very customizable, so you can choose any format you want.

      Within the product object you can find the max price, so you can use global $product to get access to that and then show the max price only in the string.

  17. subscribed. you are officially awesome

    1. Nope, you are! Thanks for subscribing!

  18. This does not change the text in the Cart though – how do I apply the same wording to the cart?

  19. I’m just writing here to let you know that we’ve recently developed a free mini extension to customize the price string of the subscription products from the backend. If you’re interested, you can download it from here: https://github.com/Prospress/woocommerce-subscriptions-custom-pricestring

    Hope it helps! 🙂

    1. Hi Bernat,
      Thanks for sharing your extension! I’ll write an article about it. I’m sure my readers will be happier to use it instead of having to write custom code!

    2. This is amazing Bernat! Thank you so much for this plugin honestly it saves so much time!!!!

  20. Hi Nicola, Is it possible for this code to only be applicable to the collection/product pages and not the cart page?

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: