WooCommerce: Edit “# in stock” @ Single Product Page

I’ve seen many snippets that change the “In Stock” text on the single product page, but not the FULL string. In this particular case, not only I needed to change the text, but also edit the order of display: from “2 in stock” to “Quantity: 2“.

WooCommerce: in stock text in the single product page
WooCommerce: in stock text in the single product page

PHP Snippet: Change “# in stock” to “Quantity: #” @ WooCommerce Single Product Page

/**
 * @snippet       Display "Quantity: #" @ WooCommerce Single Product Page
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 4.4
 * @community     https://businessbloomer.com/club/
 */
 
add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
 
function bbloomer_custom_get_availability_text( $availability, $product ) {
   $stock = $product->get_stock_quantity();
   if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Quantity: ' . $stock;
   return $availability;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

42 thoughts on “WooCommerce: Edit “# in stock” @ Single Product Page

  1. Adapting what Mark did, this time in order to show the stock message only to a given category even when we are adding stock management to all products. In my case, I needed that information only for marketplaces feed, while on a specific product tag there’s actual stock on the shop thus making it worth to show that info on the frontend.

    Here’s how I did it:

    add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
    
    function bbloomer_custom_get_availability_text( $availability, $product ) {
      global $product; // makes cat/tags info available
      $stock = $product->get_stock_quantity();
    
    
      if ( has_term( 634 , 'product_tag' ) ) { // 634 = tag ID, can be used an array for example
        if ( $product->is_in_stock() && $product->managing_stock() ) return $availability;
      } 
      else if ( $product->is_in_stock() && $product->managing_stock() ) $availability = '';
      return $availability;
    }
    
  2. Hi,
    thanks for this it was just what I needed. I added a couple of things with the help of some of the comments and other posts.
    This displays ‘Sorry, I am sold out!’ when out of stock.
    Nothing for any stock value 2 or higher.
    ‘Only 1 left!’ when stock is 1. For this message it also excludes a category, in 2 languages, as I am using WPML. This could be any array of categories though. I did not want ‘Only 1 left!’ splashed all over the Original Artworks page as there is always only 1 of each.
    Adding the global $product variable seemed to be necessary to get category exclusion to work.
    I also added the code from one of your other posts to make this appear on category pages as well as in the single product pages.

    // ADD STOCK MESSAGES TO CATEGORY AND SHOP PAGES
    add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_show_stock_shop', 10 );
    
    function bbloomer_show_stock_shop() {
      global $product;
      echo wc_get_stock_html( $product );
    }
    // CHANGE STOCK MESSAGES - APPLIES TO SHOP, CATEGORY AND SINGLE PRODUCT PAGES
    add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
    
    function bbloomer_custom_get_availability_text( $availability, $product ) {
      global $product; // TO MAKE CATEGORY AVAILABLE
      $stock = $product->get_stock_quantity();
      // OUTSIDE THE CATEGORY EXCLUSION SO THEY APPLY TO ALL 
      if ( !$product->is_in_stock() )$availability = __( 'Sorry, I am sold out!', 'mytextdomain' ); //OUT OF STOCK MESSAGE
        if ( $product->is_in_stock() && ( $stock >= 2 ) )$availability = __( '', 'mytextdomain' ); //NO MESSAGE IF STOCK OVER 1
      //EXCLUDE THESE CATEGORIES FOR 1 ITEM LEFT MESSAGE. IF THESE CATEGORIES MESSAGE IS BLANK.
      if ( has_term( array( 'ORIGINAL ARTWORKS', 'OEUVRES ORIGINALES' ), 'product_cat' ) ) {
        if ( $product->is_in_stock() && ( $stock == 1 ) )$availability = __( '', 'mytextdomain' ); //
      } //IF NOT EXCLUDED CATEGORIES MESSAGE IS 'Only 1 left!'
      else if ( $product->is_in_stock() && ( $stock == 1 ) )$availability = __( 'Only 1 left!', 'mytextdomain );
      return $availability;
    }
    
    1. I can patch together bits of PHP, I am no expert. I would be very interested in any tips from anyone to make this neater, more efficient, shorter etc.

  3. If Enable stock management at product level is not checked i would like to show some message in product page for Stock status when Stock status is in stock ” Available” , when outofstock “Out of stock” and when on backorder “Available after 4-7days” is it possible

    1. Hey Giorgio, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Is there a similar way to show how many people have placed an item in their cart?

    We have one of most items and I want people to know if there is the potential of someone else buying it.

    Thanks!

    1. Hi there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  5. Hi!. I want to change stock status displayed on product page based on shipping classes? can someone help? Thanks

    1. Hello Sarah, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. I used this to make a snip that only shows quantity on items with a stock of 2 or more.

    Thank you so much for all your time posting these, and to all the people submitting their tweaks and solutions.

    add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
    function bbloomer_custom_get_availability_text( $availability, $product ) {
    	$stock = $product->get_stock_quantity();
    	if ( $product->is_in_stock() && ($stock >= 2) ) $availability = __( 'Quantity: ' . $stock, 'woocommerce' );
    	if ( $product->is_in_stock() && ($stock <= 1) ) $availability = __( '' );
    	return $availability;
    }
    
    1. Thank you!

      1. Hi, i cannot seem to make it work.

        I used your code but i need to make a tweak with custom message and decimal stock.

        ($stock >= 5.1) -> message More then 5 in stock
        ($stock = 5) -> message 5 in stock
        ($stock message Less then 5 in stock

        add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
        function bbloomer_custom_get_availability_text( $availability, $product ) {
           $stock = $product->get_stock_quantity();
        if ( $product->is_in_stock() && ($stock >= 5.1) ) $availability = __( 'More then 5 in stock' . $stock, 'woocommerce' );
        if ( $product->is_in_stock() && ($stock = 5) ) $availability = __( '5 in stock' . $stock, 'woocommerce' );
        if ( $product->is_in_stock() && ($stock <= 4.9) ) $availability = __( 'Less then 5 in stock' . $stock, 'woocommerce' );
           return $availability;
        }
        

        But it doesnt seems to work for me.

        1. What doesn’t work exactly? Do you see anything on the screen or nothing at all?

  7. Odd, this isn’t working in any theme I throw it in.

    1. Snippet revised, it should work now πŸ™‚

  8. This worked for me but it is displaying under the product description, how do I get it to display the stock quantity next to the price?

    1. Hello Tracey, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  9. Still works. Running latest WordPress and Woocommerce. THANK YOU!

    1. Awesome!

  10. Hi,

    How do i only show this on the single product pages?
    It is now also showing on the images in the product loop and on the images in the Related products.

    Kind regards,

    Sven Heinen

    1. Hello Sven, thanks for your comment! First of all, I’ve revised the snippet to make it compatible with latest WooCommerce. Also, in regard to your question, first try the snippet and if that doesn’t fix your issue, then your theme or another plugin are adding that to the loop, and not default WooCommerce.

  11. Exactly what i was looking for BUT it does not get the right quantity. I am using it on Variable Subscription products. I use the woo-subscriptions plugin.

    1. Hello Naresh, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  12. What if we wanted to do this on a variable product? So it only shows the stock for the selected variant rather than the total of all the varients?

    1. Hey Jay, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  13. A question, if you can help me:
    currently in the product page on Inventory (list)
    I have: stock, stock exhausted and pre-order. I would like to add and “soon, let me know when it arrives” Can this be done?
    Excuse me if I did not express myself well, but I use google translation.
    Thanks ant

    1. Hello there, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  14. This is kinda an old post, I am looking to do this to a specific product category. Using slug or ID or however it can be done. That way depending on product category it can say something else. Like selling classes.

    “3” Seats available.

    and classes will be under a product category called Classes.

  15. Hello,

    I’m looking for a solution to solve the following issue:

    I sell products from two brands. Each brand has different delivery times.

    When I have the product in stock, I can deliver in 1-3 days UK, and 2-5 days Europe.

    When I don’t have the product in stock, I allow backorders. However, estimated delivery for brand A is 2-4 weeks, and brand B is 4-6 weeks.

    So basically I would like to add a condition based on the “brand” attribute to change the Backorder text.

    Thanks!

    1. Hello Rafael, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide you with a complementary solution here on the blog. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

    2. Hi Rafael,

      I had the same question from a client a couple of weeks back. They and I found it to be very helpful and I thinks it’s a solution that you might find fitting.

      https://codecanyon.net/item/woocommerce-availability-notifications/8422290

  16. Thanks alot for this one!
    How about if you want to change the ”avaliable on backorder” text on variable products? Would be awesome if you could help out! Sincerely, Vic

    1. Hey Vic, thanks for your comment! If you want to change a single string of text, check this article out: https://businessbloomer.com/translate-single-string-woocommerce-wordpress. Hope this helps!

  17. Hi Rodolfo,
    Great post!

    I was wondering what I would need to change if I would need a third message instead of two different messages (1 for in stock items and 1 for Out of stock).
    E.g.
    0 stock = Out of Stock
    1-10 stock = Only X items left, hurry!
    >10 stock = Quantity: X items

    Any help would be appreciated!
    Thanks in advance
    Koen

    1. Koen, thanks for your comment! Your query is very simple – you just need to use an additional PHP if_then statement:

      
      ...
      ...
      if ( $_product->is_in_stock() && $stock < 11 ) $availability['availability'] = "Only " . $stock . " items left, hurry!";
      ...
      ...
      
      

      Let me know πŸ™‚

  18. Thanks Rodolfo πŸ™‚

    I found lots of snippets to change the text to a fixed ‘in stock’ or ‘out of stock’ message, but was looking for a way to include the actual stock level too.

    In my case, I wanted:
    // Change stock message for ‘in stock’ products
    if ( $_product->is_in_stock() ) $availability[‘availability’] = __(‘In stock (‘ . $stock . ‘ available)’, ‘woocommerce’);

    This ‘Code Snippets’ plugin is a great way to easily add these bits of code without having to mess around with altering any php files:
    https://wordpress.org/plugins/code-snippets/

    Regards, James

    1. Thank you for your feedback James πŸ™‚

      1. Thankyou Very Much Sir Code is working perfectly

        1. Awesome Yasir, thanks so much for the feedback!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *