Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection
Renderer::enhance_product_collection_with_interactivity
Enhances the Product Collection block with client-side pagination.
This function identifies Product Collection blocks and adds necessary data attributes to enable client-side navigation. It also enqueues the Interactivity API runtime.
Method of the class: Renderer{}
No Hooks.
Returns
String. Updated block content with added interactivity attributes.
Usage
$Renderer = new Renderer(); $Renderer->enhance_product_collection_with_interactivity( $block_content, $block );
- $block_content(string) (required)
- The HTML content of the block.
- $block(array) (required)
- Block details, including its attributes.
Renderer::enhance_product_collection_with_interactivity() Renderer::enhance product collection with interactivity code WC 10.6.2
public function enhance_product_collection_with_interactivity( $block_content, $block ) {
$is_product_collection_block = $block['attrs']['query']['isProductCollectionBlock'] ?? false;
if ( $is_product_collection_block ) {
wp_enqueue_script_module( 'woocommerce/product-collection' );
$collection = $block['attrs']['collection'] ?? '';
$is_enhanced_pagination_enabled = ! ( $block['attrs']['forcePageReload'] ?? false );
$context = array(
'notices' => array(),
// Next/Previous Buttons block context.
'hideNextPreviousButtons' => false,
'isDisabledPrevious' => true,
'isDisabledNext' => false,
'ariaLabelPrevious' => __( 'Previous products', 'woocommerce' ),
'ariaLabelNext' => __( 'Next products', 'woocommerce' ),
);
if ( $collection ) {
$context['collection'] = $collection;
}
$p = new \WP_HTML_Tag_Processor( $block_content );
if ( $p->next_tag( array( 'class_name' => 'wp-block-woocommerce-product-collection' ) ) ) {
$p->set_attribute( 'data-wp-interactive', 'woocommerce/product-collection' );
$p->set_attribute( 'data-wp-init', 'callbacks.onRender' );
$p->set_attribute( 'data-wp-context', wp_json_encode( $context, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ) );
if ( $is_enhanced_pagination_enabled && isset( $this->parsed_block ) ) {
$p->set_attribute(
'data-wp-router-region',
'wc-product-collection-' . $this->parsed_block['attrs']['queryId']
);
}
}
// Check if dimensions need to be set and handle accordingly.
$this->handle_block_dimensions( $p, $block );
$block_content = $p->get_updated_html();
$block_content = $this->add_store_notices_fallback( $block_content );
}
return $block_content;
}