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

Commit e3fc564

Browse files
authored
Restore the global variable to its original value after being overriden. (#9581)
1 parent e70f419 commit e3fc564

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/BlockTypes/AddToCartForm.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ class AddToCartForm extends AbstractBlock {
2525
* @return string | void Rendered block output.
2626
*/
2727
protected function render( $attributes, $content, $block ) {
28+
global $product;
29+
2830
$post_id = $block->context['postId'];
2931

3032
if ( ! isset( $post_id ) ) {
3133
return '';
3234
}
3335

34-
$product = wc_get_product( $post_id );
36+
$previous_product = $product;
37+
$product = wc_get_product( $post_id );
3538
if ( ! $product instanceof \WC_Product ) {
39+
$product = $previous_product;
40+
3641
return '';
3742
}
3843

@@ -47,19 +52,25 @@ protected function render( $attributes, $content, $block ) {
4752
$product = ob_get_clean();
4853

4954
if ( ! $product ) {
55+
$product = $previous_product;
56+
5057
return '';
5158
}
5259

5360
$classname = $attributes['className'] ?? '';
5461
$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
5562

56-
return sprintf(
63+
$form = sprintf(
5764
'<div class="wp-block-add-to-cart-form product %1$s %2$s" style="%3$s">%4$s</div>',
5865
esc_attr( $classes_and_styles['classes'] ),
5966
esc_attr( $classname ),
6067
esc_attr( $classes_and_styles['styles'] ),
6168
$product
6269
);
70+
71+
$product = $previous_product;
72+
73+
return $form;
6374
}
6475

6576
/**

0 commit comments

Comments
 (0)