Add hydration for product data for Single Product block (https://github.com/woocommerce/woocommerce-blocks/pull/5107)

* Add hydration for product data for single product block woocommerce/woocommerce-blocks#2698

Add hydration for product data for single product block

* cast product_id variable

Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>

Co-authored-by: Albert Juhé Lluveras <contact@albertjuhe.com>
This commit is contained in:
Luigi Teschio 2021-11-09 13:58:15 +01:00 committed by GitHub
parent e019f4ae95
commit b6b275bc26
1 changed files with 20 additions and 0 deletions

View File

@ -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 @@ class SingleProduct extends AbstractBlock {
];
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" );
}
}