In WooCommerce I would like to hide Out of Stock products from Related products in single product pages. Is it possible?
Any track is appreciated.
None of the answers given here worked for me (I believe the woocommerce_output_related_products_args
filter mentioned does not accept meta_queries), and I wanted a solution that didn't use an SQL query, so I put together the solution below:
add_filter( 'woocommerce_related_products', 'mysite_filter_related_products', 10, 1 );
function mysite_filter_related_products( $related_product_ids ) {
foreach( $related_product_ids as $key => $value ) {
$relatedProduct = wc_get_product( $value );
if( ! $relatedProduct->is_in_stock() ) {
unset( $related_product_ids["$key"] );
}
}
return $related_product_ids;
}
Hope that helps someone looking for a similar solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With