A while back I wrote about how I was using WooCommerce for a site my Wife was running. It’s been running totally solid since then, with very minimal involvement from my part, which was my goal. Recently she asked a couple questions about how to achieve some things and I couldn’t find a way. They were:

  1. Charge a fee based off the category a product belonged to
  2. Only show items out of stock that were accepting backorders

The first, I turned into a plugin, available for purchase: WooCommerce – Category Fees plugin. The second, was a custom function that I wrote.

The Problem

WooCommerce as a plugin to ‘Hide out of stock items’, the problem is that it also hides out of stock items that are accepting backorders. We wanted to hide out of stock items, unless they accepted backorders.

The Solution

So heres my quick fix. First, leave the box to ‘Hide out of stock items from the catalog’ unchecked. Then paste this into the appropriate plugin or theme file:

function kfg_show_backorders( $is_visible, $id ) {
    $product = new wC_Product( $id );

    if ( ! $product->is_in_stock() && ! $product->backorders_allowed() ) {
        $is_visible = false;
    }

    return $is_visible;
}
add_filter( 'woocommerce_product_is_visible', 'kfg_show_backorders', 10, 2 );

Now, items that are out of stock that accept backorders will be visible, while out of stock items that do not accept backorders will be hidden.

Update: Commenter Oliver found out how to make variation products gray out variations that are sold out, yet keep other variations selectable in this comment below.

Posted by Chris Klosowski

Chris Klosowski is the Director of Technology at Sandhills Development, LLC, creators of fine WordPress plugins, and ecommerce solutions.

228 Comments

  1. Hi Chris,
    thank you for your fix, that’s exactly what i’m trying to do.
    But it doesn’t work for me. When I put this code in the functions.php file of my theme child, the website appears broken (white screen). Is your solution still working with the new Woocommerce version (2.4.12)?
    I really thank you if you can give me an answer.
    Have a good day.

    Reply

    1. The tips works perfectly, I did a mistake sorry and really thank you.

      Reply

    2. I am also facing the white screen issue when I added this code in file. I am getting this issue for some users not for all.

      Can you tell me what exactly you do to fix this white screen issue after adding the code.

      Reply

  2. Hi Chris, do you know of a way to hide the “out of stock” products fro showing in the related products section? Each product we sell is unique and we keep them showing in the catalog by relaxing the price by “SOLD”. What is bugging me is that they are also showing as related products. Just wondering how to to this? Thanks

    Reply

    1. I’m a bit confused by some of your quoted terms. Are you actually marking the items as Out of Stock? Are you allowing backorders? When you say ‘Relaxing the price by “SOLD”‘, what does that mean actually?

      I’m not 100% sure how to answer since some of those seem a bit different than a normal inventory workflow.

      Reply

  3. Using the woocommerce_product_is_visible hook this ruins my pagination. Only the visible products are displayed but invisible products are still counted toward pagination. pre_get_posts is not working with this script. Any ideas?

    Reply

    1. Hi,

      This is exactly what I am trying to do but like you I’m having pagination issues. Did you find a fix to this please?

      Reply

  4. I tried pasting this code into my child functions.php file and nothing had changed.
    My problem is that I have certain products I want to show even though they are out of stock. The reason is, that some products I will have a waiting list for them. I installed a plugin to do this however all the products need to be globaly set to “show when out of stock”. Since I have literally hudreds of items that are out of stock, I cannot set that variable and show all those items. So I saw your code here and thought this trick might work but I dont see any difference. I dont have that plugin you mentioned but it shouldnt matter right?
    Hey, any help would be appreciated. I have been working on this for 2 days now.
    John

    Reply

    1. Hi,

      Did you find a solution to this?

      I’ve been looking for a solution for days.

      Reply

  5. Thank you for this, I was looking for a way to do this.
    This code functions to hide products that are “out of stock” and are not on backorder, BUT unfortunately does not work for variations.

    What would be the way to hide variations which are out of stock and do not enable backorder, while still showing variations which are out of stock and do allow backorder?
    (I have a number of products where some variations are in stock and those that are out of stock can either be backordered or not, depending on the situation – I bulk edit these changes, so manually hiding each product every day is not an option).

    Thanks

    Reply

    1. Chris Klosowski October 26, 2016 at 6:51 am

      I’m not 100% positive how to do this for variations. This code works as it modifies a products visibility property. It would take a completely new set of code to apply the same type of logic to variations.

      You might try looking at this: https://www.skyverge.com/blog/add-sold-out-to-woocommerce-variable-product-dropdow/

      Reply

      1. I know I am answering myself, but through trial-and-error I got this to work by slightly modifying the code.

        Since I have not seen this solution anywhere, here it is for everyone -how to grey out variations out of stock but still show variations with backorder yes possible:

        //Grey out of stock variations but show those with backorders allowed
        add_filter( ‘woocommerce_variation_is_active’, ‘grey_out_variations_when_out_of_stock’, 10, 2 );

        function grey_out_variations_when_out_of_stock( $grey_out, $variation ) {

        if ( ! $variation->backorders_allowed() && ! $variation->is_in_stock() )
        return false;

        return true;
        }

        This works for me so I hope it works for others.

        Reply

        1. Chris Klosowski December 9, 2016 at 2:01 pm

          Nice Oliver! Thank you so much. I’ll be sure to include a link down to this comment for others to see.

          Appreciate you coming back to be sure and let us know you got it working.

          Reply

  6. Hi Chris,

    Not sure if this post is now closed but I really do need some help with the snippet.

    I used the code which appeared to be working just fine, however I recently noticed Divi shop module had random number of products showing on each page. ie number of products per page was set to 20 for example but each page would have anything but 20 products some pages 0 products, I removed this code and everything works fine again, problem is I really do need this function, any ideas on how to fix this please.

    Any help from anyone would be very much appreciatted.

    Thank you.

    Lee

    Reply

  7. I have the same problem. The allowed backorder products do not appear on the site. I am not a coder. Is there a way to uncheck the hide out of stock items directly in the edit product page?
    Thanks.

    Reply

  8. Hi Chris!!
    Great post! Maybe you can help me.
    What I need to do is to change out of stock for backorder via code.
    For example, when a product is out of stock it has to change to backordered automatically.

    Do you know how to achieve this?
    Thanks in advance.
    Best,

    Reply

  9. Hi Chrisk,
    you snippet is not working anymore on the last version of woocommerce. as soon as you add to cart a product only the header get displayed.
    I’m using flatsome theme.
    I’m soo sad, because for the filtering of the product it was doing the job and yesterday I was so happy.

    Hope you gonna ready that message.
    Thank you

    Reply

  10. Hello Chris,

    Thank you for your very handy snippet!

    Just like Jérôme reported, the snippet no longer works with the latest versions of WooCommerce (noticed on WC 3.4.3 with Royal theme). The cart and account pages go blank entirely. I had to remove the entire code from functions.php file to resolve the blank pages.

    Any idea on the way to solve this?

    Thanks!

    Reply

  11. Just discovered this snippet causes a critical error

    Error Details
    =============
    An error of type E_ERROR was caused in line 165 of the file /var/www/molinoglass.com/htdocs/wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php. Error message: Uncaught Exception: Invalid product. in /var/www/molinoglass.com/htdocs/wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php:165
    Stack trace:
    #0 /var/www/molinoglass.com/htdocs/wp-content/plugins/woocommerce/includes/class-wc-data-store.php(159): WC_Product_Data_Store_CPT->read()
    #1 /var/www/molinoglass.com/htdocs/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(135): WC_Data_Store->read()
    #2 /var/www/molinoglass.com/htdocs/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()’d code(2): WC_Product->__construct()
    #3 /var/www/molinoglass.com/htdocs/wp-includes/class-wp-hook.php(287): kfg_show_backorders()
    #4 /var/www/molinoglass.com/htdocs/wp-includes/plugin.php(206): WP_Hook->apply_filters()
    #5 /var/www/molinoglass.com/htdocs/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(1498): apply_filters()
    #6 /var/www/molinoglass.com/htdocs/wp-content/plugins/woocommerce/includes/admin/reports/class-wc-report-stock.php(139

    Reply

  12. vardenafil vs tadalafil tadalafil research tadalafil price at walmart

    Reply

  13. You can certainly see your enthusiasm in the article you write.
    The sector hopes for even more passionate writers like you who aren’t afraid to say how they believe.
    At all times follow your heart.

    Reply

  14. I need to to thank you for this great read!! I absolutely enjoyed every bit of it.
    I have got you bookmarked to look at new stuff you post…

    Reply

  15. malaria skin rash https://chloroquineorigin.com/# what is hydroxychloroquine 200 mg used for

    Reply

  16. What a data of un-ambiguity and preserveness of valuable familiarity on the topic of unpredicted emotions.

    Reply

  17. chloroquine vs chloroquine phosphate https://chloroquineorigin.com/# side effects of hydroxychloroquine

    Reply

  18. 40 mg generic cialis – cheap cialis online canadian pharmacy cialis from us pharmacy

    Reply

  19. where to purchace generic viagra how get free viagra – where to buy viagra in new york

    Reply

  20. zithromax buy zithromax for sale – where can i purchase zithromax online

    Reply

  21. 100mg doxycycline generic doxycycline – 200 mg doxycycline

    Reply

  22. ivermectin price usa stromectol 3 mg tablets price – ivermectin usa

    Reply

  23. order stromectol best price on ivermectin pills – where to buy stromectol online

    Reply

  24. soma therapy ed prescription drugs online without doctor – mens erection pills

    Reply

  25. modafinil interactions modafinil weight loss provigil 200 mg

    Reply

  26. cure ed prescription drugs online without doctor – over the counter ed

    Reply

  27. I just couldn’t leave your website before suggesting that I extremely enjoyed the standard information an individual provide on your guests? Is going to be again regularly in order to investigate cross-check new posts.

    Reply

  28. escitalopram 20 mg tablet for anxiety lexapro and pregnancy

    Reply

  29. plaquenil malaria hydroxychloroquinegnr – plaquenil

    Reply

  30. buy generic cialis cialis half life cialis 80mg

    Reply

  31. One more thing. I really believe that there are lots of travel insurance web pages of respectable companies that allow you to enter your holiday details to get you the estimates. You can also purchase your international travel cover policy online by using your current credit card. Everything you need to do is always to enter your current travel information and you can begin to see the plans side-by-side. Only find the system that suits your finances and needs and use your bank credit card to buy it. Travel insurance on the web is a good way to search for a respected company to get international travel cover. Thanks for discussing your ideas.

    Reply

  32. buy lexapro no prescription lexapro discount – lexapro buy

    Reply

  33. what are the side effects of seroquel seroquel and effexor

    Reply

  34. clomid uk buy clomid – clomid canada

    Reply

  35. I simply couldn’t leave your site before suggesting that I extremely enjoyed the standard info an individual supply to your guests? Is gonna be again incessantly in order to inspect new posts.

    Reply

  36. I was curious if you ever thought of changing the page layout of your site?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or 2 images.
    Maybe you could space it out better?

    Reply

  37. no prescription generic cialis order cialis online us – cialis online europe

    Reply

  38. viagra without a doctor prescription usa viagra over the counter – over the counter viagra

    Reply

  39. I just could not depart your site prior to suggesting that I extremely enjoyed the usual information an individual supply in your guests? Is going to be again ceaselessly in order to check up on new posts.

    Reply

  40. I like the helpful info you supply on your articles.
    I’ll bookmark your weblog and test once more right here frequently.
    I am quite certain I will be informed lots of new stuff proper right here!
    Good luck for the following!

    Reply

  41. best place to buy viagra online viagra without a doctor prescription usa – viagra cost per pill

    Reply

  42. stromectol 3mg tablets ivermectin – ivermectin 9 mg

    Reply

  43. Hello There. I found your blog using msn. That is a really well written article.
    I will make sure to bookmark it and come back to read
    more of your helpful info. Thank you for the post. I’ll
    definitely return.

    Reply

  44. After going over a number of the articles on your blog,
    I honestly appreciate your way of writing a blog. I book marked it to my
    bookmark website list and will be checking back soon. Please check out my web site too and tell me how you feel.

    Reply

  45. prednisone brand name prednisone generic – where can i buy prednisone without a prescription

    Reply

  46. I am really impressed with your writing skills as well as with the layout on your
    weblog. Is this a paid theme or did you customize
    it yourself? Either way keep up the nice quality writing, it is rare to see a nice blog like this one today.

    Reply

  47. is hydroxychloroquine an immunosuppressant where to buy chloroquine plaquenil sulfate

    Reply

  48. zithromax 500 price buy zithromax online – zithromax 250 mg pill

    Reply

  49. Hey there would you mind letting me know which webhost you’re using?
    I’ve loaded your blog in 3 completely different browsers and I
    must say this blog loads a lot quicker then most. Can you suggest
    a good internet hosting provider at a honest price?
    Cheers, I appreciate it!

    Reply

  50. Every weekend i used to go to see this site,
    because i want enjoyment, as this this web site conations actually pleasant funny material too.

    Reply

  51. This piece of writing offers clear idea in support of the new people of blogging,
    that really how to do running a blog.

    Reply

  52. cheap propecia online propecia – buy propecia finasteride

    Reply

  53. This is a topic that is close to my heart… Thank you! Where are your contact details though?

    Reply

  54. Hi to every one, the contents present at this web site are actually amazing for people experience,
    well, keep up the nice work fellows.

    Reply

  55. Have you ever thought about publishing an ebook or guest authoring
    on other sites? I have a blog based upon on the same information you discuss and would love to have
    you share some stories/information. I know
    my audience would enjoy your work. If you’re even remotely interested, feel free to shoot me an e-mail.

    Reply

  56. I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored material stylish.
    nonetheless, you command get got an nervousness over that you wish be delivering the following.
    unwell unquestionably come further formerly again since exactly the
    same nearly very often inside case you shield this hike.

    Reply

  57. ed drug comparison pills from canada – best erectile dysfunction pills

    Reply

  58. google animal porn cialis.

    Reply

  59. ivermectin 1mg WooCommerce – Hide out of stock products, only when backorders aren’t allowed | Chris Klosowski https://ivermectin1.com/ stromectol 3 mg

    Reply

  60. Hello to all, how is the whole thing, I think every one
    is getting more from this web site, and your views are fastidious for new users.

    Reply

  61. discount cialis 20mg cialis tablets in australia – buy cialis online in austalia

    Reply

  62. Instagram takipçi satın alarak fenomen olmak istemezmisiniz?

    Sizlere en iyi fırsatları sunan bu websitesinden hemen takipçi alarak düşmeyen kaliteli takipçilere sahip olabilirsiniz link :

    instagram Takipçi Satın Al

    Reply

  63. Does your website have a contact page? I’m having trouble locating it but, I’d like to shoot you an e-mail.
    I’ve got some recommendations for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it grow
    over time.

    Reply

  64. I every time spent my half an hour to read this web site’s posts everyday
    along with a mug of coffee.

    Reply

  65. erectile dysfunction cure canadian approved online pharmacies – male dysfunction

    Reply

  66. fucking goread io buy cialis.

    Reply

  67. supplements for ed legitimate canadian pharmaceuticals online – cheap erectile dysfunction pills online

    Reply

  68. I think that is among the such a lot significant info for me.
    And i am happy studying your article. But want to observation on few basic things, The web site
    taste is great, the articles is truly excellent : D. Just right
    task, cheers asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr

    Reply

  69. What’s up, the whole thing is going fine here and ofcourse every one is sharing information, that’s truly fine,
    keep up writing. quest bars http://bit.ly/3C2tkMR quest bars

    Reply

  70. Goread cialis and viagra buy.

    Reply

  71. ivermectin 3mg tablets ivermectin for sale – how much does ivermectin cost

    Reply

  72. Hi there I am so thrilled I found your weblog,
    I really found you by mistake, while I was looking on Digg for something else, Regardless I am here now and would just
    like to say kudos for a marvelous post and a all round entertaining blog (I also love the theme/design),
    I don’t have time to read through it all at the moment but I have book-marked it and
    also included your RSS feeds, so when I have time I
    will be back to read more, Please do keep up the great work.
    scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery

    Reply

  73. Goread cialis and viagra buy.

    Reply

  74. Hmm it looks like your website ate my first comment
    (it was super long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying
    your blog. I too am an aspiring blog writer but I’m still new to the whole thing.
    Do you have any recommendations for newbie blog writers? I’d definitely appreciate it.
    quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

    Reply

  75. Appreciation to my father who stated to me regarding this weblog, this blog is in fact remarkable.
    cheap flights http://1704milesapart.tumblr.com/ cheap flights

    Reply

  76. This site was… how do you say it? Relevant!! Finally I’ve found something which helped me.
    Appreciate it! ps4 games https://bit.ly/3nkdKIi ps4

    Reply

  77. Thankfulness to my father who shared with me about this blog, this
    blog is really amazing.

    Reply

  78. This post gives clear idea in favor of the new people of
    blogging, that actually how to do blogging. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis
    surgery

    Reply

  79. ivermectin price canada cheap ivermectin – ivermectin 5 mg price

    Reply

  80. Heya i’m for the first time here. I came across this board and I find It really useful & it helped me
    out much. I hope to give something back and help others like you aided me.

    Reply

  81. I am in fact delighted to glance at this webpage posts which
    contains lots of useful facts, thanks for providing such information.

    Reply

  82. I’m extremely inspired together with your writing abilities
    as neatly as with the structure for your weblog. Is that this a paid theme or did you modify it your self?

    Either way stay up the nice high quality writing, it is rare to peer
    a nice blog like this one nowadays..

    Reply

  83. solutions for ed erectional dysfunction – ed in young men

    Reply

  84. Very nice post. I just stumbled upon your
    blog and wanted to mention that I have truly enjoyed browsing your weblog
    posts. In any case I will be subscribing on your rss feed and I’m
    hoping you write once more soon!

    Reply

  85. My brother suggested I may like this website.
    He used to be totally right. This put up actually made my day.
    You cann’t consider simply how a lot time I had spent for this information! Thanks!

    Reply

  86. I couldn’t refrain from commenting. Very well written!

    Reply

  87. An intriguing discussion is worth comment. I think that you ought
    to write more about this subject, it may not be a taboo subject but usually people don’t discuss such subjects.
    To the next! Kind regards!!

    Reply

  88. My partner and I stumbled over here different web address and thought I might check things out.
    I like what I see so i am just following you. Look forward to looking over your web page yet again.

    Reply

  89. Hi friends, pleasant post and pleasant urging commented at this place, I am actually enjoying by these.

    Reply

  90. comprimidos de ivermectina reino unido http://ivermectin1.com/ ivermectin

    Reply

  91. I think the admin of this web site is truly working hard for his web page, because here every information is quality
    based information.

    Reply

  92. This is very fascinating, You are an overly skilled blogger.
    I’ve joined your feed and look ahead to in search of extra of your
    great post. Additionally, I have shared your website in my social
    networks

    Reply

  93. over the counter ed drugs canadian medications – buy ed drugs online

    Reply

  94. An impressive share! I have just forwarded this onto a co-worker who was conducting a little homework on this.
    And he in fact bought me breakfast simply because I found it for
    him… lol. So allow me to reword this…. Thank YOU for the meal!!

    But yeah, thanks for spending time to discuss this subject here on your
    web page.

    Reply

  95. Do you have a spam problem on this blog; I also
    am a blogger, and I was curious about your situation; we have created some nice procedures and we are looking to exchange techniques with other
    folks, be sure to shoot me an e-mail if interested.

    Reply

Leave a reply

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