Поля ответа «product-state» без requestId. */ public static function for_window( WC_Product $product, int $featured_id, array $gallery_ids ): array { $gallery = array_slice( array_values( array_unique( array_filter( array_map( 'intval', $gallery_ids ) ) ) ), 0, self::MAX_PHOTOS ); $image_ids = Image_Importer::image_ids( array_merge( $featured_id > 0 ? array( $featured_id ) : array(), $gallery ) ); $gallery_image_ids = array(); foreach ( $gallery as $attachment_id ) { if ( isset( $image_ids[ $attachment_id ] ) && ! in_array( $image_ids[ $attachment_id ], $gallery_image_ids, true ) ) { $gallery_image_ids[] = $image_ids[ $attachment_id ]; } } $variations = self::variations( $product ); Logger::info( 'Product state listed for the window', array( 'product_id' => $product->get_id(), 'main_image' => $featured_id, 'gallery' => count( $gallery ), 'aimodelka_photos' => count( $image_ids ), 'variations' => count( $variations ), ) ); return array( 'mainImageId' => $featured_id > 0 ? ( $image_ids[ $featured_id ] ?? null ) : null, 'galleryImageIds' => $gallery_image_ids, 'variations' => array_map( static fn ( array $variation ): array => array( 'label' => $variation['label'], 'imageId' => $variation['imageId'], 'attributes' => $variation['attributes'], ), $variations ), 'attributes' => self::attributes( $product ), 'askVariation' => Settings::ask_variation(), 'addToGalleryDefault' => Settings::variation_to_gallery(), 'message' => '', ); } /** * Вариации товара: id, название для человека, картинка aiModelka у изображения вариации * и значения атрибутов. У товара без вариаций — пусто. * * @param WC_Product $product Товар. * @return array}> */ public static function variations( WC_Product $product ): array { $rows = self::variation_rows( $product ); // Мета всех изображений вариаций — одним запросом, а не по запросу на вариацию. $images = Image_Importer::image_ids( array_column( $rows, 'attachmentId' ) ); return array_map( static fn ( array $row ): array => array( 'id' => $row['id'], 'label' => $row['label'], 'imageId' => $images[ $row['attachmentId'] ] ?? null, 'attributes' => $row['attributes'], ), $rows ); } /** * Вариации товара, как их хранит WooCommerce: id, название для человека, вложение * изображения вариации (0 — изображения нет, вариация показывает изображение товара) * и значения атрибутов. У товара без вариаций — пусто. * * Общее для окна генерации (variations() — там нужна картинка aiModelka, а не вложение) * и окошка «Упорядочить фото» на витрине (Product_Images — там нужно само вложение): название * вариации обязано писаться одинаково в обоих местах. * * @param WC_Product $product Товар. * @return array}> */ public static function variation_rows( WC_Product $product ): array { if ( ! $product->is_type( 'variable' ) ) { return array(); } $result = array(); foreach ( $product->get_children() as $variation_id ) { $variation = wc_get_product( (int) $variation_id ); if ( ! $variation || ! $variation->is_type( 'variation' ) ) { continue; } $attributes = array(); foreach ( $variation->get_attributes() as $code => $value ) { $attributes[ (string) $code ] = (string) $value; } // Название — как в WooCommerce: «Блакитний, M»; у вариации «любой / любой» — // её полное имя, иначе строка была бы пустой. $label = trim( (string) wc_get_formatted_variation( $variation, true, false, false ) ); if ( '' === $label ) { $label = (string) $variation->get_name(); } $result[] = array( 'id' => (int) $variation_id, 'label' => $label, 'attachmentId' => (int) $variation->get_image_id( 'edit' ), 'attributes' => $attributes, ); } return $result; } /** * Атрибуты вариаций товара с их значениями — для выпадающих списков в окне. Только * атрибуты, по которым сделаны вариации; у товара без вариаций — пусто. * * @param WC_Product $product Товар. * @return array}> */ public static function attributes( WC_Product $product ): array { if ( ! $product->is_type( 'variable' ) ) { return array(); } $result = array(); foreach ( $product->get_attributes() as $attribute ) { if ( ! $attribute instanceof \WC_Product_Attribute || ! $attribute->get_variation() ) { continue; } $name = (string) $attribute->get_name(); $values = array(); if ( $attribute->is_taxonomy() ) { foreach ( $attribute->get_terms() ?? array() as $term ) { if ( $term instanceof \WP_Term ) { $values[] = array( 'code' => (string) $term->slug, 'label' => (string) $term->name, ); } } } else { foreach ( $attribute->get_options() as $option ) { $option = trim( (string) $option ); if ( '' !== $option ) { $values[] = array( 'code' => $option, 'label' => $option, ); } } } $result[] = array( // Код — тот же, под которым значение лежит в мете вариации (attribute_<код>). 'code' => sanitize_title( $name ), 'label' => (string) wc_attribute_label( $name, $product ), 'values' => $values, ); } return $result; } /** * Допустимые значения атрибутов вариаций: код атрибута → коды значений. По этому * проверяется выбор из окна: чужой атрибут или чужое значение — отказ. * * @param WC_Product $product Товар. * @return array */ public static function attribute_values( WC_Product $product ): array { $result = array(); foreach ( self::attributes( $product ) as $attribute ) { $result[ $attribute['code'] ] = array_map( static fn ( array $value ): string => $value['code'], $attribute['values'] ); } return $result; } }