diff --git a/src/BlockTypes/ProductImageGallery.php b/src/BlockTypes/ProductImageGallery.php index d726bc642b2..03f9d0da54b 100644 --- a/src/BlockTypes/ProductImageGallery.php +++ b/src/BlockTypes/ProductImageGallery.php @@ -37,23 +37,56 @@ 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; + + if ( ! isset( $post_id ) ) { + return ''; + } + $product = wc_get_product( $post_id ); + if ( ! $product instanceof \WC_Product ) { + return ''; + } if ( class_exists( 'WC_Frontend_Scripts' ) ) { $frontend_scripts = new \WC_Frontend_Scripts(); $frontend_scripts::load_scripts(); } - $classname = $attributes['className'] ?? ''; + $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(); - woocommerce_show_product_sale_flash(); - $sale_badge_html = ob_get_clean(); + ?> +