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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
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 this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

40 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!

  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.

    1. Hey Sean, thanks for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know ๐Ÿ™‚

  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? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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