From 1f4a812aca16c10be860aa3caadb8a1ccca91fa2 Mon Sep 17 00:00:00 2001 From: Luigi Date: Tue, 9 Nov 2021 11:41:24 +0100 Subject: [PATCH 1/2] Add hydration for product data for single product block #2698 Add hydration for product data for single product block --- src/BlockTypes/SingleProduct.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/BlockTypes/SingleProduct.php b/src/BlockTypes/SingleProduct.php index e4fe64402c1..2d8796795a0 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 = $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" ); + } } From e4a1851cd1846c761836f95b7c96c12bbc0d5301 Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Tue, 9 Nov 2021 12:23:58 +0100 Subject: [PATCH 2/2] cast product_id variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Albert Juhé Lluveras --- src/BlockTypes/SingleProduct.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BlockTypes/SingleProduct.php b/src/BlockTypes/SingleProduct.php index 2d8796795a0..4dcadd6cea3 100644 --- a/src/BlockTypes/SingleProduct.php +++ b/src/BlockTypes/SingleProduct.php @@ -19,7 +19,7 @@ class SingleProduct extends AbstractBlock { */ protected function enqueue_assets( array $attributes ) { parent::enqueue_assets( $attributes ); - $product_id = $attributes['productId']; + $product_id = intval( $attributes['productId'] ); $this->hydrate_from_api( $product_id ); }