Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection

Controller::build_frontend_query()publicWC 1.0

Return a custom query based on attributes, filters and global WP_Query.

Method of the class: Controller{}

No Hooks.

Return

Array.

Usage

$Controller = new Controller();
$Controller->build_frontend_query( $query, $block, $page );
$query(WP_Query) (required)
The WordPress Query.
$block(WP_Block) (required)
The block being rendered.
$page(int) (required)
The page number.

Controller::build_frontend_query() code WC 9.6.0

public function build_frontend_query( $query, $block, $page ) {
	// If not in context of product collection block, return the query as is.
	$is_product_collection_block = $block->context['query']['isProductCollectionBlock'] ?? false;
	if ( ! $is_product_collection_block ) {
		return $query;
	}

	$block_context_query = $block->context['query'];

	// phpcs:ignore WordPress.DB.SlowDBQuery
	$block_context_query['tax_query'] = ! empty( $query['tax_query'] ) ? $query['tax_query'] : array();

	$inherit    = $block->context['query']['inherit'] ?? false;
	$filterable = $block->context['query']['filterable'] ?? false;

	$is_exclude_applied_filters = ! ( $inherit || $filterable );

	$collection_args = array(
		'name'                      => $block->context['collection'] ?? '',
		'productCollectionLocation' => $block->context['productCollectionLocation'] ?? null,
	);

	// Use QueryBuilder to construct the query.
	return $this->query_builder->get_final_frontend_query(
		$collection_args,
		$block_context_query,
		$page,
		$is_exclude_applied_filters
	);
}