Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection

Renderer::extend_context_for_inner_blocks │ public │ WC 1.0

Provides the location context to each inner block of the product collection block. Hint: Only blocks using the 'query' context will be affected.

The sourceData structure depends on the context type as follows:

  • site: [ ]
  • order: [ 'orderId' => int ]
  • cart: [ 'productIds' => int[] ]
  • archive: [ 'taxonomy' => string, 'termId' => int ]
  • product: [ 'productId' => int ]

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

Хуков нет.

Возвращает

array. $context {

The block context including the product collection location context.
@type array $productCollectionLocation {
	@type string  $type        The context type. Possible values are 'site', 'order', 'cart', 'archive', 'product'.
	@type array   $sourceData  The context source data. Can be the product ID of the viewed product, the order ID of the current order viewed, etc. See structure above for more details.
}

}

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

$Renderer = new Renderer();
$Renderer->extend_context_for_inner_blocks( $context, $parsed_block );
$context(массив) (обязательный)
The block context.
$parsed_block(массив)
The parsed block being rendered, used to detect the Product Collection block itself.
По умолчанию: array()

Код Renderer::extend_context_for_inner_blocks() WC 11.3.0

public function extend_context_for_inner_blocks( $context, $parsed_block = array() ) {
	// Add iapi/provider to inner blocks so they can run this store's Interactivity API actions.
	$context['iapi/provider']                 = 'woocommerce/product-collection';
	$context['isProductCollectionInnerBlock'] = true;

	$block_name = $parsed_block['blockName'] ?? '';

	if ( 'woocommerce/product-collection' === $block_name ) {
		$this->has_cart_reference_context = $this->context_provides_cart_reference( $context );

		// Set productCollectionLocation for the Product Collection block itself.
		$context['productCollectionLocation'] = $this->get_block_location_context( $context );
		return $context;
	}

	// Target only product collection's inner blocks that use the 'query' context.
	if ( ! isset( $context['query'] ) || ! isset( $context['query']['isProductCollectionBlock'] ) || ! $context['query']['isProductCollectionBlock'] ) {
		return $context;
	}

	// Check parent block contexts (in order of specificity).
	$is_in_single_product = isset( $context['singleProduct'] ) && ! empty( $context['postId'] );

	if ( $is_in_single_product ) {
		$context['productCollectionLocation'] = array(
			'type'       => self::REFERENCE_TYPE_PRODUCT,
			'sourceData' => array(
				'productId' => absint( $context['postId'] ),
			),
		);
	} else {
		$context['productCollectionLocation'] = $this->get_block_location_context( $context );
	}

	return $context;
}