WooCommerce: How to 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        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=578
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.5.1
 */

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() && get_option( 'woocommerce_stock_format' ) == '' ) $availability = __('Quantity: ' . $stock, 'woocommerce');
  return $availability;
}

Where to add this code?

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

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.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

23 thoughts on “WooCommerce: How to edit “# in stock” @ Single Product Page

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

    1. Awesome!

  2. 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.

  3. 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

  4. 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

  5. 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

  6. 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 ๐Ÿ™‚

  7. 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

  8. 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!

  9. 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 ๐Ÿ™‚

  10. 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 ๐Ÿ™‚

    2. Thankyou Very Much Sir Code is working perfectly

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

Questions? Feedback? Support? Leave your Comment Now!
_____
If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)
¯¯¯¯¯

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

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