From 0507cb54422bcdb822279ef593b0f9e7dde9404f Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Tue, 9 Nov 2021 13:58:15 +0100 Subject: [PATCH] Add hydration for product data for Single Product block (#5107) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add hydration for product data for single product block #2698 Add hydration for product data for single product block * cast product_id variable Co-authored-by: Albert Juhé Lluveras Co-authored-by: Albert Juhé Lluveras --- src/BlockTypes/SingleProduct.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/BlockTypes/SingleProduct.php b/src/BlockTypes/SingleProduct.php index e4fe64402c1..4dcadd6cea3 100644 --- a/src/BlockTypes/SingleProduct.php +++ b/src/BlockTypes/SingleProduct.php @@ -12,6 +12,17 @@ class SingleProduct extends AbstractBlock { */ protected $block_name = 'single-product'; + /** + * Enqueue frontend assets for this block, just in time for rendering. + * + * @param array $attributes Any attributes that currently are available from the block. + */ + protected function enqueue_assets( array $attributes ) { + parent::enqueue_assets( $attributes ); + $product_id = intval( $attributes['productId'] ); + $this->hydrate_from_api( $product_id ); + } + /** * Get the editor script handle for this block type. * @@ -26,4 +37,13 @@ protected function get_block_type_editor_script( $key = null ) { ]; return $key ? $script[ $key ] : $script; } + + /** + * Hydrate the cart block with data from the API. + * + * @param int $product_id ID of the product. + */ + protected function hydrate_from_api( int $product_id ) { + $this->asset_data_registry->hydrate_api_request( "/wc/store/products/$product_id" ); + } }