Woocommerce

Include only in stock in Related Products

Include only in stock in Related Products

With this snippet, you will be able to filter the related products shown, so that you dont show out of stock products.

    
// Disable out of stock products from related products add_filter('woocommerce_related_products', 'custom_filter_related_products'); function custom_filter_related_products($related_posts) { global $product; $args = array( 'posts_per_page' => -1, 'post_type' => 'product', 'post_status' => 'publish', 'post__in' => $related_posts, 'meta_query' => array( array( 'key' => '_stock_status', 'value' => 'instock', ), ), ); $filtered_related_posts = get_posts($args); $filtered_related_ids = wp_list_pluck($filtered_related_posts, 'ID'); return $filtered_related_ids; }

Leave a Reply