Created
January 21, 2023 21:52
WooCommerce remove outofstock products from related
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
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