From e6aa4c21655f96bfbe08612819ec90a32d92a7ab Mon Sep 17 00:00:00 2001 From: Patricia Hillebrandt Date: Fri, 12 May 2023 20:34:32 +0200 Subject: [PATCH 1/2] Remove global variable overwrite from Add to Cart Form block. --- src/BlockTypes/AddToCartForm.php | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/BlockTypes/AddToCartForm.php b/src/BlockTypes/AddToCartForm.php index 775876783ce..6152c4448aa 100644 --- a/src/BlockTypes/AddToCartForm.php +++ b/src/BlockTypes/AddToCartForm.php @@ -25,33 +25,28 @@ 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 ''; } - if ( ! $product instanceof \WC_Product ) { - $product = wc_get_product( $post_id ); - if ( ! $product instanceof \WC_Product ) { - return ''; - } + $single_product = wc_get_product( $post_id ); + if ( ! $single_product instanceof \WC_Product ) { + return ''; } ob_start(); - /** * Trigger the single product add to cart action for each product type. * * @since 9.7.0 */ - do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' ); + do_action( 'woocommerce_' . $single_product->get_type() . '_add_to_cart' ); - $product = ob_get_clean(); + $single_product = ob_get_clean(); - if ( ! $product ) { + if ( ! $single_product ) { return ''; } @@ -63,7 +58,7 @@ protected function render( $attributes, $content, $block ) { esc_attr( $classes_and_styles['classes'] ), esc_attr( $classname ), esc_attr( $classes_and_styles['styles'] ), - $product + $single_product ); } From 4dd7b2a1df833d442c5837136a5f776f1c06129e Mon Sep 17 00:00:00 2001 From: Patricia Hillebrandt Date: Mon, 15 May 2023 22:16:02 +0200 Subject: [PATCH 2/2] Rename single_product to product. --- src/BlockTypes/AddToCartForm.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/BlockTypes/AddToCartForm.php b/src/BlockTypes/AddToCartForm.php index 6152c4448aa..0a194d4a5b5 100644 --- a/src/BlockTypes/AddToCartForm.php +++ b/src/BlockTypes/AddToCartForm.php @@ -31,8 +31,8 @@ protected function render( $attributes, $content, $block ) { return ''; } - $single_product = wc_get_product( $post_id ); - if ( ! $single_product instanceof \WC_Product ) { + $product = wc_get_product( $post_id ); + if ( ! $product instanceof \WC_Product ) { return ''; } @@ -42,11 +42,11 @@ protected function render( $attributes, $content, $block ) { * * @since 9.7.0 */ - do_action( 'woocommerce_' . $single_product->get_type() . '_add_to_cart' ); + do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' ); - $single_product = ob_get_clean(); + $product = ob_get_clean(); - if ( ! $single_product ) { + if ( ! $product ) { return ''; } @@ -58,7 +58,7 @@ protected function render( $attributes, $content, $block ) { esc_attr( $classes_and_styles['classes'] ), esc_attr( $classname ), esc_attr( $classes_and_styles['styles'] ), - $single_product + $product ); }