Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection

Controller::update_rest_query_in_editor()publicWC 1.0

Update the query for the product query block in Editor.

Method of the class: Controller{}

No Hooks.

Return

null. Nothing (null).

Usage

$Controller = new Controller();
$Controller->update_rest_query_in_editor( $query, $request ): array;
$query(array) (required)
Query args.
$request(WP_REST_Request) (required)
Request.

Controller::update_rest_query_in_editor() code WC 9.6.0

public function update_rest_query_in_editor( $query, $request ): array {
	// Only update the query if this is a product collection block.
	$is_product_collection_block = $request->get_param( 'isProductCollectionBlock' );
	if ( ! $is_product_collection_block ) {
		return $query;
	}

	$product_collection_query_context = $request->get_param( 'productCollectionQueryContext' );
	$collection_args                  = array(
		'name'                      => $product_collection_query_context['collection'] ?? '',
		// The editor uses a REST query to grab product post types. This means we don't have a block
		// instance to work with and the client needs to provide the location context.
		'productCollectionLocation' => $request->get_param( 'productCollectionLocation' ),
	);

	// Allow collections to modify the collection arguments passed to the query builder.
	$handlers = $this->collection_handler_registry->get_collection_handler( $collection_args['name'] );
	if ( isset( $handlers['editor_args'] ) ) {
		$collection_args = call_user_func( $handlers['editor_args'], $collection_args, $query, $request );
	}

	// When requested, short-circuit the query and return the preview query args.
	$preview_state = $request->get_param( 'previewState' );
	if ( isset( $preview_state['isPreview'] ) && 'true' === $preview_state['isPreview'] ) {
		return $this->query_builder->get_preview_query_args( $collection_args, $query, $request );
	}

	$orderby             = $request->get_param( 'orderby' );
	$on_sale             = $request->get_param( 'woocommerceOnSale' ) === 'true';
	$stock_status        = $request->get_param( 'woocommerceStockStatus' );
	$product_attributes  = $request->get_param( 'woocommerceAttributes' );
	$handpicked_products = $request->get_param( 'woocommerceHandPickedProducts' );
	$featured            = $request->get_param( 'featured' );
	$time_frame          = $request->get_param( 'timeFrame' );
	$price_range         = $request->get_param( 'priceRange' );
	// This argument is required for the tests to PHP Unit Tests to run correctly.
	// Most likely this argument is being accessed in the test environment image.
	$query['author'] = '';

	// Use QueryBuilder to get the final query args.
	return $this->query_builder->get_final_query_args(
		$collection_args,
		$query,
		array(
			'orderby'             => $orderby,
			'on_sale'             => $on_sale,
			'stock_status'        => $stock_status,
			'product_attributes'  => $product_attributes,
			'handpicked_products' => $handpicked_products,
			'featured'            => $featured,
			'timeFrame'           => $time_frame,
			'priceRange'          => $price_range,
		)
	);
}