In WooCommerce, related products are a great way to cross-sell and improve customer experience. However, displaying out-of-stock items in the related products section can create confusion and frustration for customers.
WooCommerce does have a global setting to hide out-of-stock products from all views, but this approach might not suit every store, especially if you want to hide them specifically from the related products section while keeping them visible elsewhere.
In a recent Business Bloomer Club discussion, users explored various ways to implement this behavior using custom code. Here’s a comprehensive guide on how to achieve this.
Understanding Related Products in WooCommerce
WooCommerce dynamically generates related products based on shared categories or tags. While the system is automated, it doesn’t filter out-of-stock products by default. To customize this functionality, we can use a filter that modifies the query responsible for fetching related products.
Hiding Out-of-Stock Products in Related Products
The following custom code snippet allows you to exclude out-of-stock products specifically from the related products section:
add_filter( 'woocommerce_product_related_posts_query', 'filter_oos_products_from_related', 10, 3 );
function filter_oos_products_from_related( $query, $product_id, $args ) {
global $wpdb;
$query['where'] .= " AND {$wpdb->prefix}postmeta.meta_key = '_stock_status' AND {$wpdb->prefix}postmeta.meta_value = 'instock'";
$query['join'] .= " INNER JOIN {$wpdb->prefix}postmeta ON ({$wpdb->prefix}posts.ID = {$wpdb->prefix}postmeta.post_id)";
return $query;
}
How It Works
- Filter Hook:
woocommerce_product_related_posts_queryintercepts the SQL query that fetches related products. - Custom WHERE Clause: Adds a condition to include only products with
_stock_statusset toinstock. - JOIN Clause: Ensures the meta table is joined for accessing the
_stock_statusmeta key.
Testing and Debugging
After implementing the snippet, ensure to:
- Clear WooCommerce transients (
WooCommerce > Status > Tools > Clear transients). - Verify the related products display correctly for both in-stock and out-of-stock scenarios.
- Check compatibility with any plugins or custom blocks that modify the related products section.
If the snippet doesn’t work as expected, double-check for potential conflicts, such as overridden templates or caching issues.
Alternative Solutions
If you’re using plugins or custom blocks for related products, the above approach might not be effective. Instead:
- Use a Plugin: Some third-party WooCommerce plugins provide granular control over related products.
- Modify Product Visibility: Temporarily mark out-of-stock products as hidden from the catalog.
Conclusion
Removing out-of-stock products from the related products section in WooCommerce is a practical customization to enhance user experience. While the built-in settings don’t support this specific requirement, the provided code snippet offers a reliable solution.









Thanks, good post.
Could something similar be done with cross-selling and targeted selling products?
Definitely!