Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection
Renderer::extend_context_for_inner_blocks
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{}
Хуков нет.
Возвращает
Массив. $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 );
- $context(массив) (обязательный)
- The block context.
Код Renderer::extend_context_for_inner_blocks() Renderer::extend context for inner blocks WC 10.3.6
public function extend_context_for_inner_blocks( $context ) {
// Run only on frontend.
// This is needed to avoid SSR renders while in editor. @see https://github.com/woocommerce/woocommerce/issues/45181.
if ( is_admin() || \WC()->is_rest_api_request() ) {
return $context;
}
// Add iapi/provider to inner blocks so they can run this store's Interactivity API actions.
$context['iapi/provider'] = 'woocommerce/product-collection';
// 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;
}
$is_in_single_product = isset( $context['singleProduct'] ) && ! empty( $context['postId'] );
$context['productCollectionLocation'] = $is_in_single_product ? array(
'type' => 'product',
'sourceData' => array(
'productId' => absint( $context['postId'] ),
),
) : $this->get_location_context();
return $context;
}