• Hello,
    I’ve managed to alter the related product query using this filter:

    add_filter('woocommerce_product_related_posts_query', 'af_product_related_posts_query', 10, 2);
    function af_product_related_posts_query($query, $product_id){
    	
    	$out_of_stock_product_ids = get_out_of_stock_product_ids();
    	$query['where'] = $query['where'] . ' AND p.ID NOT IN ( ' . implode (",", $out_of_stock_product_ids) . ' )';
    
    	return $query;
    }

    $out_of_stock_product_ids is just an array of out of stock product ids I build using the following query:

    $args = array(
    		'post_type'			=> array( 'product' ),
    		'post_status'		=> array( 'publish' ),
    		'posts_per_page'	=> '-1',
    		'meta_query'		=>	array(
    			array(
    				'key'	=>	'_stock_status',
    				'value'	=>	'outofstock'
    			)
    		)
    	);
    		
    	$query = new WP_Query( $args );

    I prefer this than using this other solution because I always want to have the same amount of related products, avoiding the possibility to have “empty slots” of related products.

    It works well, but it looks like there’s no similare way to achieve this with upsell products.

    Anyone has ever managed to remove out of stock products from upsells?

    • This topic was modified 7 years, 1 month ago by bluantinoo.
  • The topic ‘Remove Outofstock products from Related and Upsell products’ is closed to new replies.