Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection

Controller::add_support_for_filter_blockspublicWC 1.0

Add support for filter blocks:

  • Price filter block
  • Attributes filter block
  • Rating filter block
  • In stock filter block etc.

Метод класса: Controller{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$Controller = new Controller();
$Controller->add_support_for_filter_blocks( $pre_render, $parsed_block );
$pre_render(массив) (обязательный)
The pre-rendered block.
$parsed_block(массив) (обязательный)
The parsed block.

Код Controller::add_support_for_filter_blocks() WC 10.9.4

public function add_support_for_filter_blocks( $pre_render, $parsed_block ) {
	$is_product_collection_block = $parsed_block['attrs']['query']['isProductCollectionBlock'] ?? false;

	if ( ! $is_product_collection_block ) {
		return $pre_render;
	}

	$this->renderer->set_parsed_block( $parsed_block );
	$this->asset_data_registry->add( 'hasFilterableProducts', true );
	/**
	 * It enables the page to refresh when a filter is applied, ensuring that the product collection block,
	 * which is a server-side rendered (SSR) block, retrieves the products that match the filters.
	 */
	$this->asset_data_registry->add( 'isRenderingPhpTemplate', true );

	/*
	 * When forcePageReload is enabled, the product collection has no data-wp-router-region,
	 * so the Interactivity Router cannot update it client-side. Signal the product-filters
	 * block so its navigate action falls back to a full page reload instead of using the
	 * router, without affecting other blocks on the page.
	 *
	 * This is only needed when the query is inherited from the template, as that's the
	 * only case where the Product Filters block can be a sibling rather than a descendant
	 * of the Product Collection. When it's a descendant, forcePageReload is passed through
	 * the block context instead.
	 */
	if (
		( $parsed_block['attrs']['forcePageReload'] ?? false ) &&
		( $parsed_block['attrs']['query']['inherit'] ?? false )
	) {
		wp_interactivity_config( 'woocommerce/product-filters', array( 'forcePageReload' => true ) );
	}

	return $pre_render;
}