One of the main things I enjoy as I learn more about WooCommerce is how impactful small PHP snippets can be for a specific requirement. Like always, I’m sharing what I learn as I learn. Let’s change the stock phrasing on the Single Product page…
I think of it as a duty in sharing the things I learn as a self-taught developer. We all need to support one another as we continue to learn and grow. #buildinpublic #learninpublic #indiehackers #developer
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

As I’ve mentioned in previous articles, I find great ideas in WooCommerce forums on Facebook and then attempt to implement those requests in a mock/testing environment. This makes for a solid learning experience.
In this article, I’m changing the default stock quantity phrasing or wording on the Single Product page.
Note: All custom PHP code is placed in the child theme’s functions.php file.
WooCommerce Default Stock Phrase
I’m using the default Storefront theme (actually, a child theme) with very little in the way of customization or design. The default stock quantity looks like this for a product:
To change the ‘{quantity} in stock’ phrase, you can use the add_filter() hook and target the woocommerce_get_availability_text hook:
add_filter( 'woocommerce_get_availability_text', 'woo_cust_stock_quantity_text', 99, 2 );
function woo_cust_stock_quantity_text( $availability, $product ) {
$stock = $product->get_stock_quantity();
if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Lucky you! There are ' . $stock. ' more in stock now!';
return $availability;
}
Although this example may be enough for most requirements, I wanted to try something a bit more in order to learn to custom tailor the message according to a specific product. So, in this next example, I am targeting only ‘football-jig’ slugs by testing in an if() conditional using the $product->get_slug() function:
add_filter( 'woocommerce_get_availability_text', 'woo_cust_stock_quantity_text', 99, 2 );
function woo_cust_stock_quantity_text( $availability, $product ) {
$stock = $product->get_stock_quantity();
if ( 'football-jig' == $product->get_slug() ) {
if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Lucky you! There are ' . $stock. ' more '. $product->get_name() .'s in stock now!';
}
return $availability;
}
π‘(Note: Please advise of a more practical way to filter besides the slug itself, if another more feasible option exists.)
The code is obviously not ‘bulletproof’ for this example. On a production site, I would test to see if even any amount of the product exists and also check for the total number in order to control (any) pluralization within the Stock phrasing message.
However, this should be enough to get you started with your own customizations for the stock option.
I learned so much from this fantastic resource, which enabled me to write this article: https://www.businessbloomer.com/woocommerce-edit-stock-single-product-page/
Thank you for reading this post. Please share it with someone else who would enjoy it as well.
Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.
Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.
More ways I can help
- Starting a blog? I use WordPress for the Digital Owl’s Prose blog. Letβs both save money on the plans offered. πΈ
- Get your brand, product, or service the attention it deserves with affordable classified ad placement in the OpenLampTech newsletter.
- Need hosting for your next web application or WordPress site? I highly recommend Hostinger and use them to host my niche bass fishing site. The service is second to none and they provide free SSL.
- π5 Truths I’ve Come To Realize As a Self-taught Developer
Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
π° Get your brand, product, or service the attention it deserves with affordable classified ad placement in the OpenLampTech newsletter. Thank you for your support!