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.


  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.


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


    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.


  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


    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.


  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?


    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?


  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.


    1. Hi,

      Did you find a solution to this?

      I’ve been looking for a solution for days.


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



    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/


      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.


        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.


  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.



  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?


  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.


  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


  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?



  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


  12. vardenafil vs tadalafil tadalafil research tadalafil price at walmart


  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.


  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…


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


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


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


Leave a reply

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