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:
- Charge a fee based off the category a product belonged to
- 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.