Commit 1ba3803efcf for woocommerce
commit 1ba3803efcf4b5a8e8b3cdab43eeb7942bd21358
Author: iris-woocommerce[bot] <282076412+iris-woocommerce[bot]@users.noreply.github.com>
Date: Fri Dec 4 14:49:13 2026 +0100
Separate the featured image from the gallery in product abilities (#71691)
* Mark the main product image in product abilities
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
* Keep product images unchanged when abilities write back what they read
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
* Keep a gallery main image with a missing source on write-back
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
* Keep product images the query does not show when abilities edit images
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
* Keep main image selection independent of product gallery order
Agents need to reorder a gallery without promoting its first image. Use image_id to select or clear the main image and apply images in storefront order. Keep gallery entries hidden by the read response in their stored positions so sending the same images back preserves the product.
* Expose the featured image separately from the product gallery
A combined display list loses the position of an attachment that is also in the gallery. Return the featured image with its metadata and keep gallery membership and order independent on reads and writes. Retain valid attachment IDs when a full-size URL is unavailable so agents can round-trip those images.
* Format product gallery images in stored order
The gallery getter returns an untyped list. Iterate over its attachment IDs directly so PHPStan can verify the formatter call in each product ability.
---------
Co-authored-by: aaronfc <aaron.fas@automattic.com>
Co-authored-by: matticbot <matticbot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/add-product-abilities-main-image-id b/plugins/woocommerce/changelog/add-product-abilities-main-image-id
new file mode 100644
index 00000000000..073eeefc82d
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-product-abilities-main-image-id
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Product abilities return the featured image separately from the ordered gallery. Use featured_image_id to select or clear the featured image and images to replace the gallery.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
index 9ee951da6ec..a45abd3569a 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
@@ -356,7 +356,12 @@ trait ProductAbilityTrait {
'categories' => self::get_product_id_list_input_schema( __( 'Category IDs, replacing the current ones. Empty means the default category.', 'woocommerce' ) ),
'tags' => self::get_product_id_list_input_schema( __( 'Tag IDs, replacing the current ones.', 'woocommerce' ) ),
'brands' => self::get_product_id_list_input_schema( __( 'Brand IDs, replacing the current ones.', 'woocommerce' ) ),
- 'images' => self::get_product_id_list_input_schema( __( 'Image attachment IDs, replacing the current ones. The first is the featured image.', 'woocommerce' ) ),
+ 'featured_image_id' => array(
+ 'type' => 'integer',
+ 'description' => __( 'Featured image attachment ID. Set to 0 to clear it. Omit to keep it.', 'woocommerce' ),
+ 'minimum' => 0,
+ ),
+ 'images' => self::get_product_id_list_input_schema( __( 'Gallery image attachment IDs in display order. Replaces the gallery. Use an empty list to clear it.', 'woocommerce' ) ),
'catalog_visibility' => array(
'type' => 'string',
'enum' => array_keys( wc_get_product_visibility_options() ),
@@ -564,6 +569,13 @@ trait ProductAbilityTrait {
*/
$fields = apply_filters( 'woocommerce_ability_product_type_fields', $fields, $product_type );
+ // Added after the filter so callbacks written before featured_image_id existed still get it whenever they keep images.
+ $images_position = is_array( $fields ) ? array_search( 'images', array_values( $fields ), true ) : false;
+ if ( false !== $images_position && ! in_array( 'featured_image_id', $fields, true ) ) {
+ $fields = array_values( $fields );
+ array_splice( $fields, (int) $images_position, 0, array( 'featured_image_id' ) );
+ }
+
return array(
'fields' => $fields,
'product_props' => $product_props,
@@ -742,6 +754,8 @@ trait ProductAbilityTrait {
'categories' => 'set_category_ids',
'tags' => 'set_tag_ids',
'brands' => 'set_brand_ids',
+ 'featured_image_id' => 'set_image_id',
+ 'images' => 'set_gallery_image_ids',
'catalog_visibility' => 'set_catalog_visibility',
'featured' => 'set_featured',
'date_on_sale_from' => 'set_date_on_sale_from',
@@ -776,22 +790,8 @@ trait ProductAbilityTrait {
}
}
- foreach ( array( 'images', 'dimensions' ) as $field ) {
- if ( ! in_array( $field, $product_config['fields'], true ) || ! array_key_exists( $field, $input ) ) {
- continue;
- }
-
- $prepared_value = self::prepare_product_field_value( $field, $input[ $field ] );
- if ( is_wp_error( $prepared_value ) ) {
- $refused->merge_from( $prepared_value );
- continue;
- }
-
- if ( 'images' === $field ) {
- self::set_product_images( $product, $prepared_value );
- } else {
- self::set_product_dimensions( $product, $prepared_value );
- }
+ if ( isset( $input['dimensions'] ) && in_array( 'dimensions', $product_config['fields'], true ) ) {
+ self::set_product_dimensions( $product, array_map( 'wc_format_decimal', $input['dimensions'] ) );
}
return $refused->has_errors() ? $refused : null;
@@ -1426,17 +1426,6 @@ trait ProductAbilityTrait {
);
}
- /**
- * Set the featured image from the first ID and the gallery from the rest.
- *
- * @param \WC_Product $product Product object.
- * @param array<int, int> $image_ids Attachment IDs.
- */
- private static function set_product_images( \WC_Product $product, array $image_ids ): void {
- $product->set_image_id( $image_ids[0] ?? '' );
- $product->set_gallery_image_ids( array_slice( $image_ids, 1 ) );
- }
-
/**
* Set only the dimensions in the input, so the others keep their value.
*
@@ -1567,6 +1556,15 @@ trait ProductAbilityTrait {
return self::prepare_product_image_ids( $value );
}
+ if ( 'featured_image_id' === $field ) {
+ if ( 0 === $value ) {
+ return '';
+ }
+ $image_ids = self::prepare_product_image_ids( array( $value ) );
+
+ return is_wp_error( $image_ids ) ? $image_ids : $value;
+ }
+
if ( in_array( $field, array( 'date_on_sale_from', 'date_on_sale_to' ), true ) ) {
return self::prepare_product_sale_date( $field, $value );
}
@@ -1575,10 +1573,6 @@ trait ProductAbilityTrait {
return wc_format_decimal( $value );
}
- if ( 'dimensions' === $field ) {
- return array_map( 'wc_format_decimal', $value );
- }
-
if ( 'shipping_class' === $field ) {
return self::prepare_product_shipping_class_id( $value );
}
@@ -1869,6 +1863,14 @@ trait ProductAbilityTrait {
$low_stock_amount = $product->get_low_stock_amount();
$permalink = $product->get_permalink();
+ $images = array();
+ foreach ( $product->get_gallery_image_ids() as $attachment_id ) {
+ $image = self::format_product_image_for_response( (int) $attachment_id );
+ if ( null !== $image ) {
+ $images[] = $image;
+ }
+ }
+
$data = array(
'id' => $product->get_id(),
'name' => $product->get_name(),
@@ -1899,7 +1901,7 @@ trait ProductAbilityTrait {
'featured' => (bool) $product->get_featured(),
'description' => $product->get_description(),
'short_description' => $product->get_short_description(),
- 'images' => self::format_product_images_for_response( $product ),
+ 'images' => $images,
'weight' => $product->get_weight(),
'dimensions' => array(
'length' => $product->get_length(),
@@ -1939,6 +1941,7 @@ trait ProductAbilityTrait {
$data += array_filter(
array(
+ 'featured_image' => self::format_product_image_for_response( (int) $product->get_image_id() ),
'date_on_sale_from' => self::format_date_for_response( $product->get_date_on_sale_from( 'edit' ), false ),
'date_on_sale_to' => self::format_date_for_response( $product->get_date_on_sale_to( 'edit' ), false ),
'date_created' => self::format_date_for_response( $product->get_date_created(), false ),
@@ -2081,30 +2084,25 @@ trait ProductAbilityTrait {
}
/**
- * Featured image first, then the gallery. Unlike the REST API, a product without images gets no placeholder.
+ * Keep the attachment ID even when its full-size URL is unavailable.
*
- * @param \WC_Product $product Product object.
- * @return array<int, array{id: int, src: string, name: string, alt: string}>
+ * @param int $attachment_id Image attachment ID.
+ * @return array|null
*/
- private static function format_product_images_for_response( \WC_Product $product ): array {
- $images = array();
-
- foreach ( self::get_product_image_ids( $product ) as $attachment_id ) {
- $attachment = wp_get_attachment_image_src( $attachment_id, 'full' );
-
- if ( ! is_array( $attachment ) ) {
- continue;
- }
+ private static function format_product_image_for_response( int $attachment_id ): ?array {
+ if ( ! $attachment_id || ! wp_attachment_is_image( $attachment_id ) ) {
+ return null;
+ }
- $images[] = array(
- 'id' => $attachment_id,
- 'src' => (string) $attachment[0],
- 'name' => get_post_field( 'post_title', $attachment_id ),
- 'alt' => (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
- );
+ $image = array( 'id' => $attachment_id );
+ $src = wp_get_attachment_image_src( $attachment_id, 'full' );
+ if ( is_array( $src ) ) {
+ $image['src'] = (string) $src[0];
}
+ $image['name'] = get_post_field( 'post_title', $attachment_id );
+ $image['alt'] = (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
- return $images;
+ return $image;
}
/**
@@ -2134,6 +2132,20 @@ trait ProductAbilityTrait {
* @return array
*/
private static function get_single_product_output_schema(): array {
+ $image_schema = array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'id' => array( 'type' => 'integer' ),
+ 'src' => array(
+ 'type' => 'string',
+ 'format' => 'uri',
+ ),
+ 'name' => array( 'type' => 'string' ),
+ 'alt' => array( 'type' => 'string' ),
+ ),
+ 'additionalProperties' => false,
+ );
+
return array(
'type' => 'object',
'properties' => array(
@@ -2255,22 +2267,11 @@ trait ProductAbilityTrait {
'featured' => array( 'type' => 'boolean' ),
'description' => array( 'type' => 'string' ),
'short_description' => array( 'type' => 'string' ),
+ 'featured_image' => $image_schema + array( 'description' => __( 'Featured image. Omitted when none is set.', 'woocommerce' ) ),
'images' => array(
'type' => 'array',
- 'description' => __( 'Featured image first, then the gallery.', 'woocommerce' ),
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(
- 'id' => array( 'type' => 'integer' ),
- 'src' => array(
- 'type' => 'string',
- 'format' => 'uri',
- ),
- 'name' => array( 'type' => 'string' ),
- 'alt' => array( 'type' => 'string' ),
- ),
- 'additionalProperties' => false,
- ),
+ 'description' => __( 'Gallery images in stored order.', 'woocommerce' ),
+ 'items' => $image_schema,
),
'weight' => array(
'type' => 'string',
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
index fe3dd651a78..eb7fadcbf75 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
@@ -564,8 +564,8 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
}
$description = $schema['properties']['type']['description'];
$this->assertStringContainsString( 'Fields each type takes.', $description );
- $this->assertStringContainsString( 'external: name, sku, global_unique_id, description, short_description, status, categories, tags, brands, images, catalog_visibility, featured, regular_price, sale_price, date_on_sale_from, date_on_sale_to, tax_status, external_url, button_text', $description );
- $this->assertStringContainsString( 'grouped: name, sku, global_unique_id, description, short_description, status, categories, tags, brands, images, catalog_visibility, featured, grouped_products', $description );
+ $this->assertStringContainsString( 'external: name, sku, global_unique_id, description, short_description, status, categories, tags, brands, featured_image_id, images, catalog_visibility, featured, regular_price, sale_price, date_on_sale_from, date_on_sale_to, tax_status, external_url, button_text', $description );
+ $this->assertStringContainsString( 'grouped: name, sku, global_unique_id, description, short_description, status, categories, tags, brands, featured_image_id, images, catalog_visibility, featured, grouped_products', $description );
$this->assertStringEndsWith( 'Applies to: grouped.', $schema['properties']['grouped_products']['description'] );
$this->assertStringContainsString( 'Applies to: ', $schema['properties']['regular_price']['description'] );
$this->assertStringNotContainsString( 'Applies to: ', $schema['properties']['sku']['description'] ?? '' );
@@ -2561,7 +2561,7 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
$this->assertSame( '9.00', $subscription['product']['regular_price'] );
$this->assertWPError( $bundle_price );
$this->assertSame( 'woocommerce_product_field_unsupported', $bundle_price->get_error_code() );
- $this->assertSame( 'The test-bundle product type does not take regular_price. It takes name, sku, global_unique_id, description, short_description, status, categories, tags, brands, images, catalog_visibility, featured, virtual, downloadable, weight, dimensions, shipping_class.', $bundle_price->get_error_message() );
+ $this->assertSame( 'The test-bundle product type does not take regular_price. It takes name, sku, global_unique_id, description, short_description, status, categories, tags, brands, featured_image_id, images, catalog_visibility, featured, virtual, downloadable, weight, dimensions, shipping_class.', $bundle_price->get_error_message() );
}
/**
@@ -3931,7 +3931,7 @@ class AbilitiesLoaderTest extends \WC_Unit_Test_Case {
$this->assertWPError( $result );
$this->assertSame( 'woocommerce_product_field_unsupported', $result->get_error_code() );
- $this->assertSame( 'The grouped product type does not take regular_price. It takes name, sku, global_unique_id, description, short_description, status, categories, tags, brands, images, catalog_visibility, featured, grouped_products.', $result->get_error_message() );
+ $this->assertSame( 'The grouped product type does not take regular_price. It takes name, sku, global_unique_id, description, short_description, status, categories, tags, brands, featured_image_id, images, catalog_visibility, featured, grouped_products.', $result->get_error_message() );
}
/**
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductWritesTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductWritesTest.php
index efe31a5f3fe..f35e903007d 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductWritesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductWritesTest.php
@@ -4,6 +4,7 @@ declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Internal\Abilities\Domain;
use Automattic\WooCommerce\Enums\ProductTaxStatus;
+use Automattic\WooCommerce\Enums\ProductType;
use Automattic\WooCommerce\Internal\Abilities\Domain\ProductUpdate;
use WC_Helper_Product;
use WC_Unit_Test_Case;
@@ -46,8 +47,8 @@ class ProductWritesTest extends WC_Unit_Test_Case {
$tag = wp_insert_term( 'Winter', 'product_tag' )['term_id'];
$brand = wp_insert_term( 'Acme', 'product_brand' )['term_id'];
$shipping_class = wp_insert_term( 'Bulky', 'product_shipping_class' )['term_id'];
- $featured_image = $this->create_image( 'front.jpg' );
- $gallery_image = $this->create_image( 'back.jpg' );
+ $front_image = $this->create_image( 'front.jpg' );
+ $back_image = $this->create_image( 'back.jpg' );
$created = $this->execute(
'woocommerce/product-create',
@@ -58,7 +59,7 @@ class ProductWritesTest extends WC_Unit_Test_Case {
'categories' => array( $category ),
'tags' => array( $tag ),
'brands' => array( $brand ),
- 'images' => array( $featured_image, $gallery_image ),
+ 'images' => array( $back_image, $front_image ),
'catalog_visibility' => 'search',
'featured' => true,
'date_on_sale_from' => '2030-01-01',
@@ -80,7 +81,8 @@ class ProductWritesTest extends WC_Unit_Test_Case {
$this->assertSame( array( $category ), array_column( $product['categories'], 'id' ) );
$this->assertSame( array( $tag ), array_column( $product['tags'], 'id' ) );
$this->assertSame( array( $brand ), array_column( $product['brands'], 'id' ) );
- $this->assertSame( array( $featured_image, $gallery_image ), array_column( $product['images'], 'id' ) );
+ $this->assertArrayNotHasKey( 'featured_image', $product );
+ $this->assertSame( array( $back_image, $front_image ), array_column( $product['images'], 'id' ) );
$this->assertSame( 'search', $product['catalog_visibility'] );
$this->assertTrue( $product['featured'] );
$this->assertSame( '2030-01-01T00:00:00+00:00', $product['date_on_sale_from'] );
@@ -122,12 +124,13 @@ class ProductWritesTest extends WC_Unit_Test_Case {
}
/**
- * @testdox Should remove all images and fall back to the default category when sent empty lists.
+ * @testdox Should clear the visible gallery, keep the main image and fall back to the default category when sent empty lists.
*/
- public function test_update_with_empty_lists_clears_images_and_categories(): void {
+ public function test_update_with_empty_lists_clears_gallery_and_categories(): void {
$product = WC_Helper_Product::create_simple_product();
$product->set_category_ids( array( wp_insert_term( 'Old', 'product_cat' )['term_id'] ) );
- $product->set_image_id( $this->create_image( 'old.jpg' ) );
+ $main = $this->create_image( 'old.jpg' );
+ $product->set_image_id( $main );
$product->set_gallery_image_ids( array( $this->create_image( 'older.jpg' ) ) );
$product->save();
@@ -140,10 +143,346 @@ class ProductWritesTest extends WC_Unit_Test_Case {
)
)['product'];
- $this->assertSame( array(), $updated['images'] );
+ $this->assertSame( $main, $updated['featured_image']['id'] );
+ $this->assertSame( array(), array_column( $updated['images'], 'id' ) );
$this->assertSame( array( (int) get_option( 'default_product_cat' ) ), array_column( $updated['categories'], 'id' ) );
}
+ /**
+ * @testdox Should keep a gallery-only product without a main image when its featured_image_id and images are written back as read.
+ */
+ public function test_update_round_trips_gallery_without_main_image(): void {
+ $first = $this->create_image( 'first.jpg' );
+ $second = $this->create_image( 'second.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_gallery_image_ids( array( $second, $first ) );
+ $product->save();
+
+ $read = $this->execute( 'woocommerce/products-query', array( 'id' => $product->get_id() ) )['products'][0];
+
+ $this->assertArrayNotHasKey( 'featured_image', $read );
+
+ $updated = $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $read['featured_image']['id'] ?? 0,
+ 'images' => array_column( $read['images'], 'id' ),
+ )
+ )['product'];
+
+ $stored = wc_get_product( $product->get_id() );
+ $this->assertArrayNotHasKey( 'featured_image', $updated );
+ $this->assertSame( 0, (int) $stored->get_image_id() );
+ $this->assertSame( array( $second, $first ), array_map( 'intval', $stored->get_gallery_image_ids() ) );
+ }
+
+ /**
+ * @testdox Should change only the main image when sent featured_image_id without images, and remove it when sent 0.
+ */
+ public function test_update_sets_main_image_without_touching_gallery(): void {
+ $main = $this->create_image( 'main.jpg' );
+ $gallery = $this->create_image( 'gallery.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_gallery_image_ids( array( $gallery ) );
+ $product->save();
+
+ $updated = $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $main,
+ )
+ )['product'];
+
+ $this->assertSame( $main, $updated['featured_image']['id'] );
+ $this->assertSame( array( $gallery ), array_column( $updated['images'], 'id' ) );
+
+ $removed = $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => 0,
+ )
+ )['product'];
+
+ $this->assertArrayNotHasKey( 'featured_image', $removed );
+ $this->assertSame( array( $gallery ), array_column( $removed['images'], 'id' ) );
+
+ $document = self::factory()->attachment->create_object(
+ array(
+ 'file' => 'manual.pdf',
+ 'post_mime_type' => 'application/pdf',
+ )
+ );
+ $refused = wp_get_ability( 'woocommerce/product-update' )->execute(
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $document,
+ )
+ );
+ $this->assertWPError( $refused );
+ $this->assertSame( 'woocommerce_ability_product_image_invalid', $refused->get_error_code() );
+ }
+
+ /**
+ * @testdox Should reorder the storefront gallery without changing the main image when only images is sent.
+ */
+ public function test_update_reorders_gallery_with_images_alone(): void {
+ $main = $this->create_image( 'main.jpg' );
+ $first = $this->create_image( 'first.jpg' );
+ $second = $this->create_image( 'second.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_image_id( $main );
+ $product->set_gallery_image_ids( array( $first, $second ) );
+ $product->save();
+
+ $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'images' => array( $second, $first ),
+ )
+ );
+
+ $read = $this->query_product( $product->get_id() );
+ $this->assertSame( $main, $read['featured_image']['id'] );
+ $this->assertSame( array( $second, $first ), array_column( $read['images'], 'id' ) );
+ $this->assertSame( array( $second, $first ), array_map( 'intval', wc_get_product( $product->get_id() )->get_gallery_image_ids() ) );
+ }
+
+ /**
+ * @testdox Should promote the requested gallery image and keep the old main image when it is included in images.
+ */
+ public function test_update_promotes_gallery_image_and_keeps_requested_order(): void {
+ $old_main = $this->create_image( 'old-main.jpg' );
+ $gallery = $this->create_image( 'gallery.jpg' );
+ $new_main = $this->create_image( 'new-main.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_image_id( $old_main );
+ $product->set_gallery_image_ids( array( $new_main, $gallery ) );
+ $product->save();
+
+ $updated = $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'images' => array( $gallery, $old_main, $new_main ),
+ 'featured_image_id' => $new_main,
+ )
+ )['product'];
+
+ $this->assertSame( $new_main, $updated['featured_image']['id'] );
+ $this->assertSame( array( $gallery, $old_main, $new_main ), array_column( $updated['images'], 'id' ) );
+ $this->assertSame( array( $gallery, $old_main, $new_main ), array_map( 'intval', wc_get_product( $product->get_id() )->get_gallery_image_ids() ) );
+ }
+
+ /**
+ * @testdox Should keep a main image that is also in the gallery when the query result is written back, so clearing the main image keeps it in the gallery.
+ */
+ public function test_update_round_trips_main_image_picked_from_gallery(): void {
+ $first = $this->create_image( 'first.jpg' );
+ $second = $this->create_image( 'second.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_gallery_image_ids( array( $second, $first ) );
+ $product->save();
+
+ $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $first,
+ )
+ );
+ $read = $this->execute( 'woocommerce/products-query', array( 'id' => $product->get_id() ) )['products'][0];
+ $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $read['featured_image']['id'] ?? 0,
+ 'images' => array_column( $read['images'], 'id' ),
+ )
+ );
+
+ $stored = wc_get_product( $product->get_id() );
+ $this->assertSame( $first, (int) $stored->get_image_id() );
+ $this->assertSame( array( $second, $first ), array_map( 'intval', $stored->get_gallery_image_ids() ) );
+
+ $cleared = $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => 0,
+ )
+ )['product'];
+
+ $this->assertSame( array( $second, $first ), array_column( $cleared['images'], 'id' ) );
+ }
+
+ /**
+ * @testdox Should keep a main image whose full-size source is unavailable when the query result is written back.
+ */
+ public function test_update_round_trips_main_image_without_full_size_source(): void {
+ $main = $this->create_image( 'main.jpg' );
+ $gallery = $this->create_image( 'gallery.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_image_id( $main );
+ $product->set_gallery_image_ids( array( $gallery ) );
+ $product->save();
+
+ $hide_main_source = static fn( $image, $attachment_id ) => $main === (int) $attachment_id ? false : $image;
+ add_filter( 'wp_get_attachment_image_src', $hide_main_source, 10, 2 );
+ $read = $this->execute( 'woocommerce/products-query', array( 'id' => $product->get_id() ) )['products'][0];
+ remove_filter( 'wp_get_attachment_image_src', $hide_main_source, 10 );
+
+ $this->assertSame( $main, $read['featured_image']['id'] );
+ $this->assertArrayNotHasKey( 'src', $read['featured_image'] );
+
+ $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $read['featured_image']['id'] ?? 0,
+ 'images' => array_column( $read['images'], 'id' ),
+ )
+ );
+
+ $stored = wc_get_product( $product->get_id() );
+ $this->assertSame( $main, (int) $stored->get_image_id() );
+ $this->assertSame( array( $gallery ), array_map( 'intval', $stored->get_gallery_image_ids() ) );
+ }
+
+ /**
+ * @testdox Should keep a main image that is also in the gallery when its full-size source is unavailable and the query result is written back.
+ */
+ public function test_update_round_trips_main_image_from_gallery_without_full_size_source(): void {
+ $first = $this->create_image( 'first.jpg' );
+ $second = $this->create_image( 'second.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_image_id( $first );
+ $product->set_gallery_image_ids( array( $first, $second ) );
+ $product->save();
+
+ $hide_first_source = static fn( $image, $attachment_id ) => $first === (int) $attachment_id ? false : $image;
+ add_filter( 'wp_get_attachment_image_src', $hide_first_source, 10, 2 );
+ $read = $this->execute( 'woocommerce/products-query', array( 'id' => $product->get_id() ) )['products'][0];
+ $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $read['featured_image']['id'] ?? 0,
+ 'images' => array_column( $read['images'], 'id' ),
+ )
+ );
+ remove_filter( 'wp_get_attachment_image_src', $hide_first_source, 10 );
+
+ $this->assertSame( $first, $read['featured_image']['id'] );
+ $this->assertSame( array( $first, $second ), array_column( $read['images'], 'id' ) );
+
+ $stored = wc_get_product( $product->get_id() );
+ $this->assertSame( $first, (int) $stored->get_image_id() );
+ $this->assertSame( array( $first, $second ), array_map( 'intval', $stored->get_gallery_image_ids() ) );
+ }
+
+ /**
+ * @testdox Should keep a main image in the gallery when an image is added, so clearing the main image later keeps it.
+ */
+ public function test_update_adding_an_image_keeps_main_image_in_gallery(): void {
+ $first = $this->create_image( 'first.jpg' );
+ $second = $this->create_image( 'second.jpg' );
+ $added = $this->create_image( 'added.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_image_id( $first );
+ $product->set_gallery_image_ids( array( $first, $second ) );
+ $product->save();
+
+ $read = $this->execute( 'woocommerce/products-query', array( 'id' => $product->get_id() ) )['products'][0];
+ $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $read['featured_image']['id'] ?? 0,
+ 'images' => array_merge( array_column( $read['images'], 'id' ), array( $added ) ),
+ )
+ );
+ $cleared = $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => 0,
+ )
+ )['product'];
+
+ $this->assertSame( array( $first, $second, $added ), array_column( $cleared['images'], 'id' ) );
+ }
+
+ /**
+ * @testdox Should keep a gallery image whose full-size source is unavailable when an image is added.
+ */
+ public function test_update_adding_an_image_keeps_gallery_image_without_full_size_source(): void {
+ $main = $this->create_image( 'main.jpg' );
+ $hidden = $this->create_image( 'hidden.jpg' );
+ $shown = $this->create_image( 'shown.jpg' );
+ $added = $this->create_image( 'added.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $product->set_image_id( $main );
+ $product->set_gallery_image_ids( array( $hidden, $shown ) );
+ $product->save();
+
+ $hide_source = static fn( $image, $attachment_id ) => $hidden === (int) $attachment_id ? false : $image;
+ add_filter( 'wp_get_attachment_image_src', $hide_source, 10, 2 );
+ $read = $this->execute( 'woocommerce/products-query', array( 'id' => $product->get_id() ) )['products'][0];
+ $this->execute(
+ 'woocommerce/product-update',
+ array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $read['featured_image']['id'] ?? 0,
+ 'images' => array_merge( array_column( $read['images'], 'id' ), array( $added ) ),
+ )
+ );
+ remove_filter( 'wp_get_attachment_image_src', $hide_source, 10 );
+
+ $this->assertSame( array( $hidden, $shown ), array_column( $read['images'], 'id' ) );
+
+ $stored = wc_get_product( $product->get_id() );
+ $this->assertSame( $main, (int) $stored->get_image_id() );
+ $this->assertSame( array( $hidden, $shown, $added ), array_map( 'intval', $stored->get_gallery_image_ids() ) );
+ }
+
+ /**
+ * @testdox Should accept featured_image_id for a product type exactly when its filtered field list takes images.
+ */
+ public function test_featured_image_id_follows_images_in_the_product_type_fields_filter(): void {
+ $image = $this->create_image( 'main.jpg' );
+ $product = WC_Helper_Product::create_simple_product();
+ $input = array(
+ 'id' => $product->get_id(),
+ 'featured_image_id' => $image,
+ );
+
+ $own_list = static fn( $fields, $type ) => ProductType::SIMPLE === $type ? array( 'name', 'images' ) : $fields;
+ add_filter( 'woocommerce_ability_product_type_fields', $own_list, 10, 2 );
+ try {
+ $accepted = wp_get_ability( 'woocommerce/product-update' )->execute( $input );
+ } finally {
+ remove_filter( 'woocommerce_ability_product_type_fields', $own_list, 10 );
+ }
+
+ $no_images = static fn( $fields, $type ) => ProductType::SIMPLE === $type ? array_values( array_diff( $fields, array( 'images' ) ) ) : $fields;
+ add_filter( 'woocommerce_ability_product_type_fields', $no_images, 10, 2 );
+ try {
+ $refused = wp_get_ability( 'woocommerce/product-update' )->execute( array_merge( $input, array( 'featured_image_id' => 0 ) ) );
+ } finally {
+ remove_filter( 'woocommerce_ability_product_type_fields', $no_images, 10 );
+ }
+
+ $this->assertNotWPError( $accepted );
+ $this->assertSame( $image, $accepted['product']['featured_image']['id'] );
+ $this->assertWPError( $refused );
+ $this->assertSame( 'woocommerce_product_field_unsupported', $refused->get_error_code() );
+ $this->assertSame( $image, (int) wc_get_product( $product->get_id() )->get_image_id() );
+ }
+
/**
* @testdox Should change only the dimensions sent and remove a shipping class sent as an empty slug.
*/
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php
index 9dfe850724a..c4741ab43c6 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php
@@ -163,11 +163,11 @@ class ProductsQueryTest extends WC_Unit_Test_Case {
}
/**
- * @testdox Should return the featured image first and then the gallery, without duplicates.
+ * @testdox Should identify the main image and return each image once in display order.
*/
- public function test_returns_images_in_order(): void {
- $featured = $this->create_image( 'featured.jpg', 'Featured', 'Front view' );
+ public function test_returns_unique_images_in_display_order(): void {
$gallery = $this->create_image( 'gallery.jpg', 'Gallery', '' );
+ $featured = $this->create_image( 'featured.jpg', 'Featured', 'Front view' );
$product = WC_Helper_Product::create_simple_product();
$product->set_image_id( $featured );
$product->set_gallery_image_ids( array( $gallery, $featured ) );
@@ -175,10 +175,11 @@ class ProductsQueryTest extends WC_Unit_Test_Case {
$item = $this->query_single( $product->get_id() );
- $this->assertSame( array( $featured, $gallery ), array_column( $item['images'], 'id' ) );
- $this->assertSame( 'Featured', $item['images'][0]['name'] );
- $this->assertSame( 'Front view', $item['images'][0]['alt'] );
- $this->assertStringEndsWith( 'featured.jpg', $item['images'][0]['src'] );
+ $this->assertSame( $featured, $item['featured_image']['id'] );
+ $this->assertSame( array( $gallery, $featured ), array_column( $item['images'], 'id' ) );
+ $this->assertSame( 'Featured', $item['featured_image']['name'] );
+ $this->assertSame( 'Front view', $item['featured_image']['alt'] );
+ $this->assertStringEndsWith( 'featured.jpg', $item['featured_image']['src'] );
}
/**
@@ -192,7 +193,7 @@ class ProductsQueryTest extends WC_Unit_Test_Case {
$item = $this->query_single( $product->get_id() );
- $this->assertSame( 'Bob\'s mug -- "x"', $item['images'][0]['name'] );
+ $this->assertSame( 'Bob\'s mug -- "x"', $item['featured_image']['name'] );
}
/**