is_product_edit_screen() ) { return; } $suffix = Constants::is_true( 'SCRIPT_DEBUG' ) ? '' : '.min'; wp_enqueue_script( self::SCRIPT_HANDLE, \WC()->plugin_url() . '/assets/js/admin/variation-gallery' . $suffix . '.js', array( 'wc-admin-variation-meta-boxes', 'wp-a11y' ), Constants::get_constant( 'WC_VERSION' ), true ); wp_localize_script( self::SCRIPT_HANDLE, 'wcVariationGalleryL10n', array( 'manageTitle' => __( 'Manage variation gallery', 'woocommerce' ), 'manageButton' => __( 'Update gallery', 'woocommerce' ), 'replaceTitle' => __( 'Replace image', 'woocommerce' ), 'replaceButton' => __( 'Use this image', 'woocommerce' ), 'replaceLabel' => __( 'Replace', 'woocommerce' ), 'addTitle' => __( 'Add images to variation gallery', 'woocommerce' ), 'addButton' => __( 'Add to gallery', 'woocommerce' ), 'emptyCtaLabel' => __( 'Add variation images', 'woocommerce' ), 'announceUpdated' => __( 'Variation gallery updated.', 'woocommerce' ), 'announceReplaced' => __( 'Image replaced.', 'woocommerce' ), 'announceRemoved' => __( 'Image removed from variation gallery.', 'woocommerce' ), 'announceReorder' => __( 'Variation gallery order updated.', 'woocommerce' ), 'announcePrimary' => __( 'New primary image set.', 'woocommerce' ), 'removeLabel' => __( 'Remove image', 'woocommerce' ), 'countZero' => __( 'No images yet', 'woocommerce' ), /* translators: %d: number of variation gallery images */ 'countSingular' => __( '%d image', 'woocommerce' ), /* translators: %d: number of variation gallery images */ 'countPlural' => __( '%d images', 'woocommerce' ), 'primaryLabel' => __( 'Primary', 'woocommerce' ), /* translators: %d: gallery image position */ 'thumbLabel' => __( 'Show gallery image %d', 'woocommerce' ), 'missingFileLabel' => __( 'Attachment file missing', 'woocommerce' ), ) ); wp_enqueue_style( self::STYLE_HANDLE, \WC()->plugin_url() . '/assets/css/variation-gallery-admin.css', array(), Constants::get_constant( 'WC_VERSION' ) ); } /** * Render the variation gallery field. * * @param int $loop Variation row index. * @param array $variation_data Variation data. * @param WP_Post $variation Variation post object. * @return void */ public function render_variation_gallery_field( int $loop, array $variation_data, WP_Post $variation ): void { $variation_object = wc_get_product( $variation->ID ); if ( ! $variation_object instanceof WC_Product_Variation ) { return; } $image_ids = $this->get_display_image_ids( $variation_object ); $count = count( $image_ids ); $field_id = 'variable_gallery_image_ids_' . $loop; $hero_id = $count > 0 ? $image_ids[0] : 0; ?> set_image_id( $featured_id ); $variation->set_gallery_image_ids( $gallery_ids ); LegacyVariationGalleryCompatibility::mark_core_managed( $variation ); } catch ( \Throwable $e ) { Telemetry::record_event( Telemetry::EVENT_SAVE_FAILED, array( 'context' => 'classic_admin', 'reason' => get_class( $e ), ) ); throw $e; } Telemetry::record_event( Telemetry::EVENT_SAVE_SUCCEEDED, array( 'context' => 'classic_admin', 'image_count' => count( $unified_ids ), 'is_multi' => count( $unified_ids ) > 1 ? 'yes' : 'no', ) ); } /** * Display-time image list (render-only). * * Prepends the variation's featured image to the gallery when it's not * already present, so the meta-box UI shows a single ordered set instead * of two separate fields. This synthesis is **not persisted** — storage * only changes when the merchant saves the variation, at which point * `gallery[0]` becomes the canonical primary image. * * @param WC_Product_Variation $variation Variation object. * @return array */ private function get_display_image_ids( WC_Product_Variation $variation ): array { $image_ids = array_values( array_map( 'intval', $variation->get_gallery_image_ids() ) ); // Read the stored value so an inherited parent image is not saved as the variation's own image. $featured_id = (int) $variation->get_image_id( 'edit' ); if ( $featured_id > 0 && ! in_array( $featured_id, $image_ids, true ) ) { array_unshift( $image_ids, $featured_id ); } if ( ! empty( $image_ids ) ) { _prime_post_caches( $image_ids ); } return $image_ids; } /** * Render the hero image. * * @param int $image_id Attachment ID. * @return void */ private function render_hero_image( int $image_id ): void { $html = wp_get_attachment_image( $image_id, 'woocommerce_single', false, array( 'class' => 'wc-variation-gallery-field__hero-img', 'data-id' => (string) $image_id, 'decoding' => 'async', 'loading' => 'lazy', ) ); if ( '' === $html ) { ?>
  • id; } }