Skip to content

Instantly share code, notes, and snippets.

@robin-scott
Last active January 30, 2022 12:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robin-scott/2cf176ed4507ea1c3f9b35ef404668ea to your computer and use it in GitHub Desktop.
Save robin-scott/2cf176ed4507ea1c3f9b35ef404668ea to your computer and use it in GitHub Desktop.
Remove out of stock products from WooCommerce Related products
// remove OOS products from related products in WooCommerce, because they are OOS! by Robin Scott of silicondales.com - see more at https://silicondales.com/tutorials/woocommerce/remove-out-of-stock-products-from-woocommerce-related-products/
add_filter( 'woocommerce_related_products', 'exclude_oos_related_products', 10, 3 );
function exclude_oos_related_products( $related_posts, $product_id, $args ){
$out_of_stock_product_ids = (array) wc_get_products( array(
'status' => 'publish',
'limit' => -1,
'stock_status' => 'outofstock',
'return' => 'ids',
) );
$exclude_ids = $out_of_stock_product_ids;
return array_diff( $related_posts, $exclude_ids );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment