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

Commit

Permalink
Restore the global variable to its original value after being overrid…
Browse files Browse the repository at this point in the history
…en. (#9581)
  • Loading branch information
nefeline authored May 24, 2023
1 parent e70f419 commit e3fc564
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/BlockTypes/AddToCartForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ class AddToCartForm extends AbstractBlock {
* @return string | void Rendered block output.
*/
protected function render( $attributes, $content, $block ) {
global $product;

$post_id = $block->context['postId'];

if ( ! isset( $post_id ) ) {
return '';
}

$product = wc_get_product( $post_id );
$previous_product = $product;
$product = wc_get_product( $post_id );
if ( ! $product instanceof \WC_Product ) {
$product = $previous_product;

return '';
}

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

if ( ! $product ) {
$product = $previous_product;

return '';
}

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

return sprintf(
$form = sprintf(
'<div class="wp-block-add-to-cart-form product %1$s %2$s" style="%3$s">%4$s</div>',
esc_attr( $classes_and_styles['classes'] ),
esc_attr( $classname ),
esc_attr( $classes_and_styles['styles'] ),
$product
);

$product = $previous_product;

return $form;
}

/**
Expand Down

0 comments on commit e3fc564

Please sign in to comment.