ID, self::META, true ) ) ) ); ?>
$full[0], 'src' => $main[0], 'width' => $main[1], 'height' => $main[2], 'thumb' => $thumb ? $thumb[0] : $main[0], 'srcset' => (string) wp_get_attachment_image_srcset( $id, 'woocommerce_single' ), 'sizes' => (string) wp_get_attachment_image_sizes( $id, 'woocommerce_single' ), 'alt' => (string) get_post_meta( $id, '_wp_attachment_image_alt', true ), ); } private static function items( $ids ) { return array_values( array_filter( array_map( array( __CLASS__, 'image_data' ), $ids ) ) ); } public static function variation_data( $data, $product, $variation ) { $ids = array_filter( array_map( 'absint', explode( ',', (string) get_post_meta( $variation->get_id(), self::META, true ) ) ) ); // Put the variation's own image first so it always leads its gallery. if ( $ids && $variation->get_image_id() ) { array_unshift( $ids, $variation->get_image_id() ); $ids = array_values( array_unique( $ids ) ); } $data['variantly_gallery'] = self::items( $ids ); $data['variantly_badge'] = (string) get_post_meta( $variation->get_id(), '_variantly_badge', true ); return $data; } /* ---------- Frontend ---------- */ public static function replace_gallery() { remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 ); add_action( 'woocommerce_before_single_product_summary', array( __CLASS__, 'render' ), 20 ); // Block themes lay the product out with blocks, so swap the gallery block's output in place. add_filter( 'render_block_woocommerce/product-image-gallery', array( __CLASS__, 'render_block' ), 10, 3 ); } public static function render_block( $content, $parsed_block, $block ) { $post_id = isset( $block->context['postId'] ) ? $block->context['postId'] : get_the_ID(); $product = wc_get_product( $post_id ); if ( ! $product || ! $product->is_type( 'variable' ) ) { return $content; } ob_start(); self::output( $product ); return ob_get_clean(); } public static function render() { global $product; if ( wp_is_block_theme() ) { return; // render_block() handles it. } if ( ! $product || ! $product->is_type( 'variable' ) ) { woocommerce_show_product_images(); return; } self::output( $product ); } private static function output( $product ) { self::enqueue(); $ids = array_filter( array_merge( array( $product->get_image_id() ), $product->get_gallery_image_ids() ) ); $items = self::items( $ids ); if ( ! $items ) { $items = array( array( 'full' => wc_placeholder_img_src( 'full' ), 'src' => wc_placeholder_img_src(), 'thumb' => wc_placeholder_img_src(), 'srcset' => '', 'sizes' => '', 'alt' => '', 'width' => 600, 'height' => 600 ) ); } ?>