Nicola Mustone

Happiness Lead @ Automattic



How to Edit the Out of Stock String Conditionally

I’ve recently watched The Queen’s Gambit on Netflix and that has awoken in me the will to play some more chess again as an adult and learn more about it, and be fascinated by this beautiful game again. I have always liked chess since I was a child. I can remember spending time playing it with my cousin at home. It was such a hard game back then, I could barely understand the moves.

So I started looking for chess sets that I could purchase online and I stumbled upon a website from UK which had most of their sets, boards, and pieces, sold out. The Out of Stock string read “Out of Stock because of The Queen’s Gambit”.

At first, I found that funny. I then explored further the website to find out that some products were priced over 1000 GBP and still out of stock because of the TV show. I find it quite hard to believe that someone who watched the show decided to spend over 1000 GBP for a chess board or some pieces. This gave me the idea to write this article.

How can you conditionally change the Out of Stock string based on the product price? Let’s find out!

Setting Up The Board Code

Before you make any code changes, have a backup of your site. This will help you restore things in case of unforeseen errors. I recommend using Jetpack Backup for real-time backups and one-click restores.

As usual, we need to edit some code, so open your wp-content/themes/your-child-theme-name/functions.php file and add this code at the end of it:

This code is simple and quick to change. On line 3 you can set the maximum product’s price. Anything below that value will use the custom Out of stock string instead of the default one. Make sure to also change the “Out of stock” string in the condition on line 3 to your current string, in case you are using a language different than English.

On line 4 instead, you can set your custom Out of stock string. The result will look like this:

Part of the single product page showing a custom Out of stock string.

This little trick allows you to gauge interest and keep momentum with your sales, like this chess shop did, but in a more realistic way!

How are you using this snippet? What is your custom Out of stock string, and how is it helping you? Let me know in the comments!


Subscribe to This Blog

Receive new articles from this blog directly in your inbox! No spam guaranteed!

Join 670 other subscribers

Leave a Reply

You Might Also Like These Articles


Contribute to Improving This Blog

Did you enjoy this article? Was it helpful? Contribute to help me write more articles! The funds collected from this blog are reinvested directly into improving my skills or the blog so that I can provide more and better content!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

€1.00
€5.00
€10.00
€5.00
€15.00
€100.00
€5.00
€15.00
€100.00

Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Don't Miss a Thing!

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Join 670 other subscribers

Continue Reading

Discover more from Nicola Mustone

Subscribe now to keep reading and get access to the full archive.

Continue reading