From 33cc492aa79d25b6ce876ddb7ad0580441637a17 Mon Sep 17 00:00:00 2001 From: Patricia Hillebrandt Date: Fri, 12 May 2023 22:27:11 +0200 Subject: [PATCH 1/2] Remove call to the global variable and add support for the gallery block to continue to work within the Single Product template without any problems. --- src/BlockTypes/ProductImageGallery.php | 55 +++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/src/BlockTypes/ProductImageGallery.php b/src/BlockTypes/ProductImageGallery.php index d726bc642b2..5d433360851 100644 --- a/src/BlockTypes/ProductImageGallery.php +++ b/src/BlockTypes/ProductImageGallery.php @@ -37,10 +37,17 @@ protected function get_block_type_uses_context() { * @return string Rendered block type output. */ protected function render( $attributes, $content, $block ) { - $post_id = $block->context['postId']; - global $product; - $product = wc_get_product( $post_id ); + + if ( ! isset( $post_id ) ) { + return ''; + } + + $single_post = get_post( $post_id ); + $single_product = wc_get_product( $post_id ); + if ( ! $single_product instanceof \WC_Product ) { + return ''; + } if ( class_exists( 'WC_Frontend_Scripts' ) ) { $frontend_scripts = new \WC_Frontend_Scripts(); @@ -49,11 +56,49 @@ protected function render( $attributes, $content, $block ) { $classname = $attributes['className'] ?? ''; ob_start(); - woocommerce_show_product_sale_flash(); + if ( $single_product->is_on_sale() ) { + echo apply_filters( 'woocommerce_sale_flash', '' . esc_html__( 'Sale!', 'woocommerce' ) . '', $single_post, $single_product ); + } + $sale_badge_html = ob_get_clean(); ob_start(); - woocommerce_show_product_images(); + $columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 ); + $post_thumbnail_id = $single_product->get_image_id(); + $wrapper_classes = apply_filters( + 'woocommerce_single_product_image_gallery_classes', + array( + 'woocommerce-product-gallery', + 'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ), + 'woocommerce-product-gallery--columns-' . absint( $columns ), + 'images', + ) + ); + ?> +
+ '; + } + + echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', $html, $post_thumbnail_id ); + + $attachment_ids = $single_product->get_gallery_image_ids(); + + if ( $attachment_ids && $single_product->get_image_id() ) { + foreach ( $attachment_ids as $attachment_id ) { + echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', wc_get_gallery_image_html( $attachment_id ), $attachment_id ); + } + } + ?> +
+ + Date: Tue, 16 May 2023 00:08:06 +0200 Subject: [PATCH 2/2] Make essential/required changes to guarantee the Product Gallery block works as expected without the relying on the global product. --- src/BlockTypes/ProductImageGallery.php | 46 ++++++++++---------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/src/BlockTypes/ProductImageGallery.php b/src/BlockTypes/ProductImageGallery.php index 5d433360851..03f9d0da54b 100644 --- a/src/BlockTypes/ProductImageGallery.php +++ b/src/BlockTypes/ProductImageGallery.php @@ -43,9 +43,8 @@ protected function render( $attributes, $content, $block ) { return ''; } - $single_post = get_post( $post_id ); - $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 ''; } @@ -54,26 +53,17 @@ protected function render( $attributes, $content, $block ) { $frontend_scripts::load_scripts(); } - $classname = $attributes['className'] ?? ''; - ob_start(); - if ( $single_product->is_on_sale() ) { - echo apply_filters( 'woocommerce_sale_flash', '' . esc_html__( 'Sale!', 'woocommerce' ) . '', $single_post, $single_product ); - } - - $sale_badge_html = ob_get_clean(); - - ob_start(); - $columns = apply_filters( 'woocommerce_product_thumbnails_columns', 4 ); - $post_thumbnail_id = $single_product->get_image_id(); - $wrapper_classes = apply_filters( - 'woocommerce_single_product_image_gallery_classes', - array( - 'woocommerce-product-gallery', - 'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ), - 'woocommerce-product-gallery--columns-' . absint( $columns ), - 'images', - ) + $classname = $attributes['className'] ?? ''; + $sale_badge_html = $product->is_on_sale() ? '' . esc_html__( 'Sale!', 'woo-gutenberg-products-block' ) . '' : ''; + $columns = 4; + $post_thumbnail_id = $product->get_image_id(); + $wrapper_classes = array( + 'woocommerce-product-gallery', + 'woocommerce-product-gallery--' . ( $post_thumbnail_id ? 'with-images' : 'without-images' ), + 'woocommerce-product-gallery--columns-' . absint( $columns ), + 'images', ); + ob_start(); ?>