-
Notifications
You must be signed in to change notification settings - Fork 215
Add single product block #8610
Add single product block #8610
Changes from 1 commit
a963247
150e090
c4376f3
f168cf8
558da33
270ed81
cf5e905
ad7b3d1
bddcba8
f7ffd01
85975ee
9683707
aea6171
810e578
af44329
134673c
44f8d48
c597452
2b2e967
e8f0485
6aa5dcd
b662d51
a9865ab
b926dbc
09d22b9
b41c45d
d48885e
7ce210b
daeb00c
bdc22ad
c57f265
8298400
321b3d7
79b86bc
5492bd5
4fffeb5
2567373
5cf6e9e
9af8b21
422423b
2c2b34b
5b2334a
15f46e8
e32db09
aabd3f2
8d9f3bd
583e49e
b28d10e
650cb93
4c93233
0a94903
b5a6591
815f6d1
317145e
9b024ce
b877f79
adbe4e9
14c87c7
64aec59
7dfa554
3596e22
930a814
9095527
07ab19e
b97bd02
e3fe1c0
ecaadec
3b66307
3610324
c9233af
83bb365
6d9dab8
6e6ecd8
8cc11a4
561dc19
9f83f35
f77ecee
2f824f3
675f3b1
b87d0a5
fe807b9
31ad081
cd9f9f8
8bb4d52
667827c
dadf2dc
d32d65e
7d61f35
ab6a2bb
e39d65e
1e90f34
70dfe54
5703ec1
c1bd5dc
93e8698
a7f84c7
70f2892
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ class SingleProduct extends AbstractBlock { | |
| */ | ||
| protected $block_name = 'single-product'; | ||
| protected $product_id = 0; | ||
| protected $single_product_inner_blocks_names = []; | ||
|
|
||
| /** | ||
| * API version name. | ||
|
|
@@ -57,17 +58,47 @@ protected function render( $attributes, $content, $block ) { | |
| * @return array Updated block context. | ||
| */ | ||
| public function update_context( $context, $block, $parent_block ) { | ||
| // var_dump($block['blockName']); | ||
|
|
||
| if( $block['blockName'] === 'woocommerce/single-product' | ||
| && isset( $block['attrs']['productId'] ) ) { | ||
| $this->product_id = $block['attrs']['productId']; | ||
|
|
||
| $this->single_product_inner_blocks_names = array_reverse($this->extract_single_product_inner_block_names( $block )); | ||
| } | ||
| if( $this->product_id > 0 ){ | ||
| global $post; | ||
| $post = get_post( $this->product_id ); | ||
| setup_postdata( $post ); | ||
| $context['postId'] = $this->product_id; | ||
| } | ||
|
|
||
| $this->replace_post_for_single_product_inner_block( $block, $context ); | ||
|
|
||
| return $context; | ||
| } | ||
|
|
||
| protected function extract_single_product_inner_block_names($block, &$result = []) { | ||
| if(isset($block['blockName'])){ | ||
| $result[] = $block['blockName']; | ||
| } | ||
|
|
||
| if(isset($block['innerBlocks'])){ | ||
| foreach ($block['innerBlocks'] as $inner_block) { | ||
| $this->extract_single_product_inner_block_names($inner_block, $result); | ||
| } | ||
| } | ||
| return $result; | ||
| } | ||
|
|
||
| protected function replace_post_for_single_product_inner_block( $block, &$context ) { | ||
| if( $this->single_product_inner_blocks_names ){ | ||
| $block_name = array_pop( $this->single_product_inner_blocks_names); | ||
|
|
||
| if( $block_name === $block['blockName']){ | ||
| global $post; | ||
| $post = get_post( $this->product_id ); | ||
|
||
| setup_postdata( $post ); | ||
| $context['postId'] = $this->product_id; | ||
| } | ||
|
|
||
| if ( ! $this->single_product_inner_blocks_names ) { | ||
| wp_reset_postdata(); | ||
| } | ||
| } | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.