Fix "out of stock" products count on archives in WooCommerce

/**
 * fix_hide_out_of_stock_products.
 */
add_action( 'pre_get_posts', 'fix_hide_out_of_stock_products' );
function fix_hide_out_of_stock_products( $q ) {
	if ( ! $q->is_main_query() || is_admin() || ! $q->is_archive() ) {
		return;
	}
	$meta_query   = ( array ) $q->get( 'meta_query' );
	$meta_query[] = array(
		'key'       => '_stock_status',
		'value'     => 'outofstock',
		'compare'   => 'NOT IN'
	);
	$q->set( 'meta_query', $meta_query );
	remove_action( 'pre_get_posts', 'fix_hide_out_of_stock_products' );
}

No comments yet.

Leave a Reply