is_type( 'variable' ) ) { return; } wp_enqueue_script( 'wcvg-frontend', WCVG_PLUGIN_URL . 'assets/js/wcvg-frontend.js', array( 'jquery', 'wc-single-product' ), WCVG_VERSION, true ); } public function add_gallery_to_variation_data( $data, $product, $variation ) { $gallery_raw = $variation->get_meta( '_wcvg_gallery_ids' ); if ( ! $gallery_raw || ! function_exists( 'wc_get_gallery_image_html' ) ) { $data['wcvg_gallery_html'] = ''; $data['wcvg_gallery_images'] = array(); return $data; } $ids = array_filter( array_map( 'absint', explode( ',', $gallery_raw ) ) ); $html = ''; $images = array(); foreach ( $ids as $index => $id ) { if ( ! wp_attachment_is_image( $id ) ) { continue; } // wc_get_gallery_image_html() is WooCommerce's own template // function (used to build the product's default gallery) — using // it here guarantees our markup, classes and data attributes // (data-large_image, data-large_image_width/height, etc.) match // exactly what the installed WooCommerce version and its zoom / // slider scripts expect, instead of us guessing at the structure // by hand and drifting out of sync on a future WooCommerce update. $html .= wc_get_gallery_image_html( $id, 0 === $index ); // Themes that replace the native gallery entirely (WoodMart, // Woostify, and others) don't understand the markup above at // all — they never render a `.woocommerce-product-gallery` // element for us to swap HTML into in the first place. For // those, the frontend script needs the raw image data instead // so it can build each theme's own expected markup itself. // Kept as a plain array (not HTML) so it's safe/reusable // regardless of which theme-specific integration consumes it. $full = wp_get_attachment_image_src( $id, 'full' ); $large = wp_get_attachment_image_src( $id, 'woocommerce_single' ); $thumb = wp_get_attachment_image_src( $id, 'woocommerce_gallery_thumbnail' ); $images[] = array( 'id' => $id, 'full' => $full ? $full[0] : '', 'full_w' => $full ? $full[1] : 0, 'full_h' => $full ? $full[2] : 0, 'src' => $large ? $large[0] : ( $full ? $full[0] : '' ), 'w' => $large ? $large[1] : 0, 'h' => $large ? $large[2] : 0, 'srcset' => wp_get_attachment_image_srcset( $id, 'woocommerce_single' ), 'sizes' => wp_get_attachment_image_sizes( $id, 'woocommerce_single' ), 'thumb' => $thumb ? $thumb[0] : ( $large ? $large[0] : '' ), 'alt' => get_post_meta( $id, '_wp_attachment_image_alt', true ), ); } $data['wcvg_gallery_html'] = $html; $data['wcvg_gallery_images'] = $images; return $data; } }