A powerful membership solution for WordPress

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:

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.

Post Promoter Pro

Posted by Chris Klosowski

Hi, I'm Chris Klosowski. Currently I am a Lead Developer of Easy Digital Downloads, where we build the easiest way to sell digital products with WordPress.

I am also the person behind Post Promoter Pro, the most effective way to promote your WordPress Content.

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

Leave a reply

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