Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Add hydration for product data for Single Product block (#5107)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

Co-authored-by: Albert Juhé Lluveras <[email protected]>
  • Loading branch information
gigitux and Aljullu authored Nov 9, 2021
1 parent 1661f24 commit b88d367
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/BlockTypes/SingleProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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" );
}
}

0 comments on commit b88d367

Please sign in to comment.