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.
Метод класса: Renderer{}
Хуков нет.
Возвращает
Строку
. Updated block content with added interactivity attributes.
Использование
$Renderer = new Renderer(); $Renderer->enhance_product_collection_with_interactivity( $block_content, $block );
- $block_content(строка) (обязательный)
- The HTML content of the block.
- $block(массив) (обязательный)
- Block details, including its attributes.
Код Renderer::enhance_product_collection_with_interactivity() Renderer::enhance product collection with interactivity WC 9.8.5
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() ); 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; }