Automattic\WooCommerce\Blocks

QueryFilters::get_stock_status_counts()publicWC 1.0

Get stock status counts for the current products.

Method of the class: QueryFilters{}

No Hooks.

Return

Array. status=>count pairs.

Usage

$QueryFilters = new QueryFilters();
$QueryFilters->get_stock_status_counts( $query_vars );
$query_vars(array) (required)
The WP_Query arguments.

QueryFilters::get_stock_status_counts() code WC 8.9.0

public function get_stock_status_counts( $query_vars ) {
	global $wpdb;
	$stock_status_options = array_map( 'esc_sql', array_keys( wc_get_product_stock_status_options() ) );

	add_filter( 'posts_clauses', array( $this, 'add_query_clauses' ), 10, 2 );
	add_filter( 'posts_pre_query', '__return_empty_array' );

	$query_vars['no_found_rows']  = true;
	$query_vars['posts_per_page'] = -1;
	$query_vars['fields']         = 'ids';
	$query                        = new \WP_Query();
	$result                       = $query->query( $query_vars );
	$product_query_sql            = $query->request;

	remove_filter( 'posts_clauses', array( $this, 'add_query_clauses' ), 10 );
	remove_filter( 'posts_pre_query', '__return_empty_array' );

	$stock_status_counts = array();

	foreach ( $stock_status_options as $status ) {
		$stock_status_count_sql = $this->generate_stock_status_count_query( $status, $product_query_sql, $stock_status_options );

		$result = $wpdb->get_row( $stock_status_count_sql ); // phpcs:ignore
		$stock_status_counts[ $status ] = $result->status_count;
	}

	return $stock_status_counts;
}