Custom Stock Phrasing On WooCommerce Single Product Page

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…

Custom WooCommerce and Shopify Solutions

Discover useful WooCommerce and Shopify custom solutions for your online store today at affordable prices!

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.

Background image by 3D Animation Production Company from Pixabay 

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

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. I appreciate your support!


2 thoughts on “Custom Stock Phrasing On WooCommerce Single Product Page

  1. Hi there!

    Thank you very very much! You are my hero of the day.

    I adjusted your snippet to make it work with tags:

    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 ( has_term( 1100 , ‘product_tag’ ) ) {
    if ( $product->is_in_stock() && $product->managing_stock() ) $availability = ‘Custom tekst here ‘ . $stock. ‘your custom tekst here’;
    }
    return $availability;
    }

    Many many thanks!

    • So cool! That’s awesome! I am still learning WooCommerce too and this is very helpful and informative. Be sure and give praise and visit the Business Bloomer website too as that’s where I learn a lot of WooCommerce because that guy is so knowledgeable in Woo. Thanks for reading and the comments!

Hey thanks for commenting! Leave a Reply

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