Last active
January 30, 2022 12:14
-
-
Save robin-scott/2cf176ed4507ea1c3f9b35ef404668ea to your computer and use it in GitHub Desktop.
Remove out of stock products from WooCommerce Related products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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