Commit 688c843a1e2 for woocommerce

commit 688c843a1e2d23c76e25ce9005552c95e8677b62
Author: Bero <1199991+bgrgicak@users.noreply.github.com>
Date:   Mon Nov 16 09:01:56 2026 +0100

    Add gallery images to product structured data (#70355)

    * Add gallery images to product structured data

    * Add changelog entry for product gallery schema

    * Add product schema gallery image tests

    * Preserve product schema filter compatibility

    * Simplify product schema gallery checks

    * Remove unnecessary gallery filter guards

    * Simplify product gallery schema changes

diff --git a/plugins/woocommerce/changelog/add-product-schema-gallery-images b/plugins/woocommerce/changelog/add-product-schema-gallery-images
new file mode 100644
index 00000000000..e84854537b1
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-product-schema-gallery-images
@@ -0,0 +1,4 @@
+Significance: patch
+Type: enhancement
+
+Include product gallery images in structured data for shopping agents.
diff --git a/plugins/woocommerce/includes/class-wc-structured-data.php b/plugins/woocommerce/includes/class-wc-structured-data.php
index 293465c1bb2..9f23a5a4e6e 100644
--- a/plugins/woocommerce/includes/class-wc-structured-data.php
+++ b/plugins/woocommerce/includes/class-wc-structured-data.php
@@ -20,6 +20,7 @@ use Automattic\WooCommerce\Enums\DimensionUnit;
 use Automattic\WooCommerce\Enums\WeightUnit;
 use Automattic\WooCommerce\Enums\ProductStockStatus;
 use Automattic\WooCommerce\Enums\TaxDisplayMode;
+use Automattic\WooCommerce\Internal\Utilities\ProductUtil;
 use Automattic\WooCommerce\Internal\StructuredData\ShippingDestinations;

 defined( 'ABSPATH' ) || exit;
@@ -238,7 +239,17 @@ class WC_Structured_Data {
 		$shop_url  = home_url();
 		$currency  = get_woocommerce_currency();
 		$permalink = get_permalink( $product->get_id() );
-		$image     = wp_get_attachment_url( (int) $product->get_image_id() );
+		wc_get_container()->get( ProductUtil::class )->prime_image_caches( array( $product ) );
+		$image   = wp_get_attachment_url( (int) $product->get_image_id() );
+		$gallery = $product->get_gallery_image_ids();
+		$gallery = array_unique( array_filter( array_map( 'intval', $gallery ) ) );
+		$images  = $image ? array( $image ) : array();
+		foreach ( $gallery as $image_id ) {
+			$gallery_image = wp_attachment_is_image( $image_id ) ? wp_get_attachment_url( $image_id ) : false;
+			if ( $gallery_image && ! in_array( $gallery_image, $images, true ) ) {
+				$images[] = $gallery_image;
+			}
+		}

 		$markup = array(
 			'@type'       => 'Product',
@@ -248,8 +259,8 @@ class WC_Structured_Data {
 			'description' => wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) ),
 		);

-		if ( $image ) {
-			$markup['image'] = $image;
+		if ( $images ) {
+			$markup['image'] = 1 === count( $images ) ? $images[0] : $images;
 		}

 		// Declare SKU or fallback to ID.
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php b/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php
index 09b37e46256..93f2c8ad5e7 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-structured-data-test.php
@@ -351,6 +351,70 @@ class WC_Structured_Data_Test extends \WC_Unit_Test_Case {
 		$this->assertEquals( $this->structured_data->prepare_gtin( '123.4e-5' ), '12345' );
 	}

+	/**
+	 * @testdox Product schema includes unique gallery images after the featured image.
+	 */
+	public function test_product_schema_includes_gallery_images(): void {
+		$featured  = $this->create_schema_attachment( 'featured.jpg' );
+		$gallery   = $this->create_schema_attachment( 'gallery.jpg' );
+		$duplicate = $this->create_schema_attachment( 'gallery.jpg' );
+		$pdf       = $this->create_schema_attachment( 'document.pdf', 'application/pdf' );
+		$invalid   = self::factory()->post->create();
+
+		$product = WC_Helper_Product::create_simple_product();
+		$product->set_image_id( $featured );
+		$product->set_gallery_image_ids( array( $gallery, $featured, $duplicate, $pdf, $invalid ) );
+		$this->structured_data->generate_product_data( $product );
+
+		$this->assertSame(
+			array( wp_get_attachment_url( $featured ), wp_get_attachment_url( $gallery ) ),
+			$this->structured_data->get_data()[0]['image'],
+			'The featured image should precede unique, valid gallery URLs.'
+		);
+	}
+
+	/**
+	 * @testdox Product schema preserves single-image output and supports a gallery without a featured image.
+	 * @testWith [true, false]
+	 *           [false, true]
+	 *           [false, false]
+	 *
+	 * @param bool $featured Whether to set a featured image.
+	 * @param bool $gallery Whether to set a gallery image.
+	 */
+	public function test_product_schema_with_one_or_no_image( bool $featured, bool $gallery ): void {
+		$image   = $this->create_schema_attachment( 'product.jpg' );
+		$product = WC_Helper_Product::create_simple_product();
+		$product->set_image_id( $featured ? $image : 0 );
+		$product->set_gallery_image_ids( $gallery ? array( $image ) : array() );
+		$this->structured_data->generate_product_data( $product );
+
+		$data = $this->structured_data->get_data()[0];
+		if ( $featured || $gallery ) {
+			$this->assertSame( wp_get_attachment_url( $image ), $data['image'] );
+		} else {
+			$this->assertArrayNotHasKey( 'image', $data );
+		}
+	}
+
+	/**
+	 * Create an attachment for product schema tests.
+	 *
+	 * @param string $file Attachment file name.
+	 * @param string $mime_type Attachment MIME type.
+	 * @return int Attachment ID.
+	 */
+	private function create_schema_attachment( string $file, string $mime_type = 'image/jpeg' ): int {
+		$id = self::factory()->post->create(
+			array(
+				'post_type'      => 'attachment',
+				'post_mime_type' => $mime_type,
+			)
+		);
+		update_post_meta( $id, '_wp_attached_file', $file );
+		return $id;
+	}
+
 	/**
 	 * @testdox Product schema includes usable gallery videos in order and omits them when disabled or without thumbnails.
 	 */