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 President of Easy Digital Downloads, the easiest way to sell your digital products with WordPress. Trusted by over 50,000 smart store owners.