Commit 02932a4b86a for woocommerce

commit 02932a4b86aab7aaeb66201c22e55e099ab43850
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date:   Thu Nov 19 10:49:50 2026 -0300

    Read, write and filter by the product GTIN in the product abilities (#71074)

    * Read and write the product GTIN through the product abilities

    products-query, product-create and product-update now return and accept
    global_unique_id, on products and on variations. A duplicate GTIN, a GTIN
    with letters, or a variation that repeats the GTIN of its parent or of a
    sibling is refused.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Let products-query find products by GTIN

    products-query takes a global_unique_id filter that matches the GTIN
    exactly. Without a type or parent_id filter, it also searches
    variations, so an agent finds the variation that carries the GTIN.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Let products-query filter by several GTINs at once

    The global_unique_id filter now takes a list of GTINs and returns the
    products and variations whose GTIN equals any of them.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Treat x and X as the same GTIN check digit between variations

    Two variations sent in one call with the ISBN-10 values 080442957X and
    080442957x passed the sibling check, because the check compared the raw
    strings. It now compares them without case. The GTIN input pattern is
    also defined once and shared by the write fields and the query filter.

    Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

    * Leave the GTIN X position check to Core and bound the GTIN filter

    The schema pattern now only checks the characters the setter keeps, so a
    misplaced X gets Core's own error message. The products-query GTIN filter
    refuses empty strings, takes at most 100 GTINs, and says that a category,
    tag or brand filter drops the variations.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    * Share the GTIN character set with WC_Product and reject a final newline

    The ability schema, the setter and the importer now read the characters a
    GTIN may contain from WC_Product::GLOBAL_UNIQUE_ID_CHARACTERS. The schema
    pattern ends with (?![\s\S]) instead of $, so a GTIN of "\n" no longer
    passes and clears the stored value.

    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

    ---------

    Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>

diff --git a/plugins/woocommerce/changelog/add-wooai-1615-product-gtin-abilities b/plugins/woocommerce/changelog/add-wooai-1615-product-gtin-abilities
new file mode 100644
index 00000000000..37b5ad413f0
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-wooai-1615-product-gtin-abilities
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Let the product abilities read, write and filter by the product GTIN, UPC, EAN or ISBN.
diff --git a/plugins/woocommerce/includes/abstracts/abstract-wc-product.php b/plugins/woocommerce/includes/abstracts/abstract-wc-product.php
index c2e66495bf7..043526c28af 100644
--- a/plugins/woocommerce/includes/abstracts/abstract-wc-product.php
+++ b/plugins/woocommerce/includes/abstracts/abstract-wc-product.php
@@ -35,6 +35,13 @@ require_once WC_ABSPATH . 'includes/legacy/abstract-wc-legacy-product.php';
 class WC_Product extends WC_Abstract_Legacy_Product {
 	use CogsAwareTrait;

+	/**
+	 * Regex character class of the characters a GTIN may contain: digits, hyphens, and X/x for the ISBN-10 check digit.
+	 *
+	 * @since 11.4.0
+	 */
+	public const GLOBAL_UNIQUE_ID_CHARACTERS = '0-9Xx\-';
+
 	/**
 	 * This is the name of this object type.
 	 *
@@ -959,8 +966,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
 	 * @return void
 	 */
 	public function set_global_unique_id( $global_unique_id ) {
-		// Strip characters that are never valid (digits, hyphens, and X/x for the ISBN-10 check digit).
-		$global_unique_id = preg_replace( '/[^0-9Xx\-]/', '', (string) $global_unique_id );
+		$global_unique_id = preg_replace( '/[^' . self::GLOBAL_UNIQUE_ID_CHARACTERS . ']/', '', (string) $global_unique_id );

 		// X is only valid as the final ISBN-10 check digit character (ISO 2108) — reject anywhere else.
 		if ( $this->get_object_read() && ! empty( $global_unique_id ) && ! preg_match( '/^[0-9\-]*[0-9Xx]?$/', $global_unique_id ) ) {
diff --git a/plugins/woocommerce/includes/import/abstract-wc-product-importer.php b/plugins/woocommerce/includes/import/abstract-wc-product-importer.php
index eda6a0c869f..4b6113da66f 100644
--- a/plugins/woocommerce/includes/import/abstract-wc-product-importer.php
+++ b/plugins/woocommerce/includes/import/abstract-wc-product-importer.php
@@ -259,7 +259,7 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
 	 * @since 11.2.0
 	 */
 	protected function normalize_global_unique_id( $global_unique_id ) {
-		return (string) preg_replace( '/[^0-9Xx\-]/', '', (string) $global_unique_id );
+		return (string) preg_replace( '/[^' . WC_Product::GLOBAL_UNIQUE_ID_CHARACTERS . ']/', '', (string) $global_unique_id );
 	}

 	/**
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php
index 734631f47f0..a855f6cc371 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/ProductsQuery.php
@@ -142,6 +142,10 @@ class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {
 			$args['sku'] = wc_clean( $input['sku'] );
 		}

+		if ( ! empty( $input['global_unique_id'] ) ) {
+			$args['global_unique_id'] = $input['global_unique_id'];
+		}
+
 		if ( isset( $input['type'] ) ) {
 			$args['type'] = $input['type'];
 			$parent_types = self::get_variation_subclass_parent_types()[ $input['type'] ] ?? array();
@@ -157,6 +161,10 @@ class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {
 			$args['type']   = $args['type'] ?? ProductType::VARIATION;
 		}

+		if ( ! empty( $args['global_unique_id'] ) && ! isset( $args['type'] ) ) {
+			$args['type'] = array_merge( array_keys( wc_get_product_types() ), array( ProductType::VARIATION ) );
+		}
+
 		foreach ( array( 'virtual', 'downloadable' ) as $field ) {
 			if ( isset( $input[ $field ] ) ) {
 				$args[ $field ] = $input[ $field ];
@@ -302,46 +310,56 @@ class ProductsQuery extends AbstractDomainAbility implements AbilityDefinition {
 		$schema = array(
 			'type'                 => 'object',
 			'properties'           => array(
-				'id'           => array(
+				'id'               => array(
 					'type'    => 'integer',
 					'minimum' => 1,
 				),
-				'search'       => array( 'type' => 'string' ),
-				'sku'          => array(
+				'search'           => array( 'type' => 'string' ),
+				'sku'              => array(
 					'type'        => 'string',
 					'description' => __( 'Limit results to products with SKUs that partially match this string. Use * to match products with any non-empty SKU.', 'woocommerce' ),
 				),
-				'status'       => array(
+				'global_unique_id' => array(
+					'type'        => 'array',
+					'description' => __( 'Exact GTINs, UPCs, EANs or ISBNs. Matches any of them. Finds variations too, unless category, tag or brand is also sent.', 'woocommerce' ),
+					'maxItems'    => 100,
+					'items'       => array(
+						'type'      => 'string',
+						'minLength' => 1,
+						'pattern'   => self::get_global_unique_id_input_pattern(),
+					),
+				),
+				'status'           => array(
 					'type' => 'string',
 					'enum' => self::get_product_query_status_slugs(),
 				),
-				'type'         => self::get_product_type_schema( array_values( array_unique( array_merge( array_keys( self::get_supported_product_type_configs() ), array_keys( self::get_variation_subclass_parent_types() ) ) ) ) ),
-				'parent_id'    => array(
+				'type'             => self::get_product_type_schema( array_values( array_unique( array_merge( array_keys( self::get_supported_product_type_configs() ), array_keys( self::get_variation_subclass_parent_types() ) ) ) ) ),
+				'parent_id'        => array(
 					'type'        => 'integer',
 					'description' => __( 'Limit results to the variations of this variable product.', 'woocommerce' ),
 					'minimum'     => 1,
 				),
-				'virtual'      => array(
+				'virtual'          => array(
 					'type'        => 'boolean',
 					'description' => __( 'Limit results to products that are not shipped (true) or that are shipped (false).', 'woocommerce' ),
 				),
-				'downloadable' => array(
+				'downloadable'     => array(
 					'type'        => 'boolean',
 					'description' => __( 'Limit results to products that have files to download (true) or that do not (false).', 'woocommerce' ),
 				),
-				'stock_status' => array(
+				'stock_status'     => array(
 					'type' => 'string',
 					'enum' => array_keys( wc_get_product_stock_status_options() ),
 				),
-				'category'     => self::get_term_filter_input_schema( __( 'Only products in any of these product category IDs, including their child categories.', 'woocommerce' ) ),
-				'tag'          => self::get_term_filter_input_schema( __( 'Only products with any of these product tag IDs.', 'woocommerce' ) ),
-				'brand'        => self::get_term_filter_input_schema( __( 'Only products of any of these brand IDs, including their child brands.', 'woocommerce' ) ),
-				'page'         => array(
+				'category'         => self::get_term_filter_input_schema( __( 'Only products in any of these product category IDs, including their child categories.', 'woocommerce' ) ),
+				'tag'              => self::get_term_filter_input_schema( __( 'Only products with any of these product tag IDs.', 'woocommerce' ) ),
+				'brand'            => self::get_term_filter_input_schema( __( 'Only products of any of these brand IDs, including their child brands.', 'woocommerce' ) ),
+				'page'             => array(
 					'type'    => 'integer',
 					'default' => 1,
 					'minimum' => 1,
 				),
-				'per_page'     => array(
+				'per_page'         => array(
 					'type'    => 'integer',
 					'default' => 10,
 					'minimum' => 1,
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
index c2ebe234f62..5c94053135c 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php
@@ -288,6 +288,11 @@ trait ProductAbilityTrait {
 			'attributes'         => self::get_product_attributes_schema( $for_variation ),
 			'variations'         => array( 'type' => 'array' ),
 			'sku'                => array( 'type' => 'string' ),
+			'global_unique_id'   => array(
+				'type'        => 'string',
+				'description' => __( 'GTIN, UPC, EAN or ISBN, unique in the store. Empty removes it.', 'woocommerce' ),
+				'pattern'     => self::get_global_unique_id_input_pattern(),
+			),
 			'regular_price'      => array(
 				'type'        => 'string',
 				'description' => __( 'Decimal price as a string, without a currency symbol or thousand separators.', 'woocommerce' ),
@@ -469,7 +474,7 @@ trait ProductAbilityTrait {
 	 */
 	protected static function get_variation_config(): array {
 		return array(
-			'fields'        => array( 'sku', 'description', 'status', 'virtual', 'downloadable', 'attributes', 'regular_price', 'sale_price', 'manage_stock', 'stock_quantity', 'stock_status' ),
+			'fields'        => array( 'sku', 'global_unique_id', 'description', 'status', 'virtual', 'downloadable', 'attributes', 'regular_price', 'sale_price', 'manage_stock', 'stock_quantity', 'stock_status' ),
 			'product_props' => array(),
 		);
 	}
@@ -617,7 +622,7 @@ trait ProductAbilityTrait {
 	 * @return array<int, string>
 	 */
 	private static function get_common_product_mutation_fields(): array {
-		return array( 'name', 'sku', 'description', 'short_description', 'status', 'categories', 'tags', 'brands', 'images', 'catalog_visibility', 'featured' );
+		return array( 'name', 'sku', 'global_unique_id', 'description', 'short_description', 'status', 'categories', 'tags', 'brands', 'images', 'catalog_visibility', 'featured' );
 	}

 	/**
@@ -711,6 +716,7 @@ trait ProductAbilityTrait {
 			'parent_id'          => 'set_parent_id',
 			'name'               => 'set_name',
 			'sku'                => 'set_sku',
+			'global_unique_id'   => 'set_global_unique_id',
 			'regular_price'      => 'set_regular_price',
 			'sale_price'         => 'set_sale_price',
 			'description'        => 'set_description',
@@ -1066,10 +1072,11 @@ trait ProductAbilityTrait {
 	/**
 	 * Check that no variation picks the same options as another variation in
 	 * the list or a saved variation of the parent, and that no variation in the
-	 * list carries the SKU of the parent or of another variation in the list. A
-	 * saved variation that the input names is left out, because the input
-	 * changes or deletes it. `set_sku()` already checks a SKU against the saved
-	 * products, but not against the parent's SKU before the parent is saved.
+	 * list carries the SKU or GTIN of the parent or of another variation in the
+	 * list. A saved variation that the input names is left out, because the
+	 * input changes or deletes it. `set_sku()` and `set_global_unique_id()`
+	 * already check a value against the saved products, but not against the
+	 * parent's value before the parent is saved.
 	 *
 	 * @param \WC_Product_Variable    $parent_product Parent product.
 	 * @param \WC_Product_Variation[] $variations     Variations to save, in input order.
@@ -1080,6 +1087,8 @@ trait ProductAbilityTrait {
 		$combinations = array();
 		$parent_sku   = $parent_product->get_sku( 'edit' );
 		$skus         = '' === $parent_sku ? array() : array( $parent_sku => true );
+		$parent_gtin  = $parent_product->get_global_unique_id( 'edit' );
+		$gtins        = '' === $parent_gtin ? array() : array( strtoupper( $parent_gtin ) => true );
 		$child_ids    = $parent_product->get_id() > 0 ? self::read_variation_children( $parent_product, true )['all'] : array();
 		_prime_post_caches( $child_ids );
 		foreach ( $child_ids as $child_id ) {
@@ -1111,6 +1120,17 @@ trait ProductAbilityTrait {
 				);
 			}
 			$skus[ $sku ] = true;
+
+			$gtin = strtoupper( $variation->get_global_unique_id( 'edit' ) );
+			if ( '' !== $gtin && isset( $gtins[ $gtin ] ) ) {
+				return new \WP_Error(
+					'woocommerce_product_variation_global_unique_id_duplicate',
+					/* translators: %d: variation position. */
+					sprintf( __( 'Variation %d repeats the GTIN of the product or of another variation. Give each one its own GTIN.', 'woocommerce' ), $position ),
+					array( 'status' => 400 )
+				);
+			}
+			$gtins[ $gtin ] = true;
 		}

 		return null;
@@ -1653,6 +1673,16 @@ trait ProductAbilityTrait {
 		return $term->term_id;
 	}

+	/**
+	 * Characters a GTIN input may contain. The setter would strip any other character, so the schema
+	 * refuses it instead. The pattern ends with (?![\s\S]) because $ also matches before a final newline.
+	 *
+	 * @return string
+	 */
+	private static function get_global_unique_id_input_pattern(): string {
+		return '^[' . \WC_Product::GLOBAL_UNIQUE_ID_CHARACTERS . ']*(?![\s\S])';
+	}
+
 	/**
 	 * Get a schema-compatible pattern for non-negative weight and dimension inputs.
 	 *
@@ -1811,6 +1841,7 @@ trait ProductAbilityTrait {
 			'type'               => $product->get_type(),
 			'status'             => $product->get_status(),
 			'sku'                => $product->get_sku(),
+			'global_unique_id'   => $product->get_global_unique_id(),
 			'currency'           => get_woocommerce_currency(),
 			'currency_symbol'    => html_entity_decode(
 				get_woocommerce_currency_symbol(),
@@ -2088,6 +2119,7 @@ trait ProductAbilityTrait {
 					'enum' => self::get_product_output_status_slugs(),
 				),
 				'sku'                => array( 'type' => 'string' ),
+				'global_unique_id'   => array( 'type' => 'string' ),
 				'currency'           => array(
 					'type' => 'string',
 					'enum' => array_keys( get_woocommerce_currencies() ),
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
index 70d13f692f3..8aeb099942f 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/AbilitiesLoaderTest.php
@@ -520,8 +520,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, 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, 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, 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->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'] ?? '' );
@@ -2355,7 +2355,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, 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, images, catalog_visibility, featured, virtual, downloadable, weight, dimensions, shipping_class.', $bundle_price->get_error_message() );
 	}

 	/**
@@ -3646,7 +3646,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, 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, 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 2126d36ec9e..49c74af581c 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductWritesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductWritesTest.php
@@ -247,6 +247,173 @@ class ProductWritesTest extends WC_Unit_Test_Case {
 		$this->assertSame( $shipping_class, $variation->get_shipping_class_id() );
 	}

+	/**
+	 * @testdox Should set and read back the GTIN of a product and of its variations.
+	 */
+	public function test_global_unique_id_round_trips(): void {
+		$created = $this->execute(
+			'woocommerce/product-create',
+			array(
+				'name'             => 'Mug',
+				'global_unique_id' => '4006381333931',
+			)
+		)['product'];
+
+		$this->assertSame( '4006381333931', $this->query_product( $created['id'] )['global_unique_id'] );
+
+		$updated = $this->execute(
+			'woocommerce/product-update',
+			array(
+				'id'               => $created['id'],
+				'global_unique_id' => '978-3-16-148410-X',
+			)
+		)['product'];
+
+		$this->assertSame( '978-3-16-148410-X', $updated['global_unique_id'] );
+
+		$variable = $this->execute(
+			'woocommerce/product-create',
+			array(
+				'name'             => 'Cap',
+				'type'             => 'variable',
+				'global_unique_id' => '0012345678905',
+				'attributes'       => array(
+					array(
+						'name'    => 'Size',
+						'options' => array( 'S', 'M' ),
+					),
+				),
+				'variations'       => array(
+					array(
+						'attributes'       => array(
+							array(
+								'name'   => 'Size',
+								'option' => 'S',
+							),
+						),
+						'global_unique_id' => '0012345678912',
+					),
+				),
+			)
+		)['product'];
+
+		$this->assertSame( '0012345678905', $variable['global_unique_id'] );
+		$this->assertSame( '0012345678912', $this->query_product( $variable['variation_ids'][0] )['global_unique_id'] );
+	}
+
+	/**
+	 * @testdox Should refuse a GTIN that another product has, that a variation repeats, or that has letters.
+	 */
+	public function test_rejects_duplicate_or_invalid_global_unique_id(): void {
+		$taken = WC_Helper_Product::create_simple_product();
+		$taken->set_global_unique_id( '4006381333931' );
+		$taken->save();
+		$product = WC_Helper_Product::create_simple_product();
+
+		$duplicate = wp_get_ability( 'woocommerce/product-update' )->execute(
+			array(
+				'id'               => $product->get_id(),
+				'global_unique_id' => '4006381333931',
+			)
+		);
+
+		$this->assertWPError( $duplicate );
+		$this->assertSame( 'product_invalid_global_unique_id', $duplicate->get_error_code() );
+		$this->assertSame( '', wc_get_product( $product->get_id() )->get_global_unique_id() );
+
+		$letters = wp_get_ability( 'woocommerce/product-update' )->execute(
+			array(
+				'id'               => $product->get_id(),
+				'global_unique_id' => 'ABC123',
+			)
+		);
+
+		$this->assertWPError( $letters );
+
+		$misplaced_x = wp_get_ability( 'woocommerce/product-update' )->execute(
+			array(
+				'id'               => $product->get_id(),
+				'global_unique_id' => '08044X2957',
+			)
+		);
+
+		$this->assertWPError( $misplaced_x );
+		$this->assertSame( 'product_invalid_global_unique_id_format', $misplaced_x->get_error_code() );
+
+		$product->set_global_unique_id( '5901234123457' );
+		$product->save();
+		foreach ( array( "\n", "5901234123464\n" ) as $newline ) {
+			$result = wp_get_ability( 'woocommerce/product-update' )->execute(
+				array(
+					'id'               => $product->get_id(),
+					'global_unique_id' => $newline,
+				)
+			);
+
+			$this->assertWPError( $result );
+			$this->assertSame( '5901234123457', wc_get_product( $product->get_id() )->get_global_unique_id() );
+		}
+
+		$size      = static fn( string $option ): array => array(
+			array(
+				'name'   => 'Size',
+				'option' => $option,
+			),
+		);
+		$repeating = wp_get_ability( 'woocommerce/product-create' )->execute(
+			array(
+				'name'             => 'Cap',
+				'type'             => 'variable',
+				'global_unique_id' => '0012345678905',
+				'attributes'       => array(
+					array(
+						'name'    => 'Size',
+						'options' => array( 'S', 'M' ),
+					),
+				),
+				'variations'       => array(
+					array(
+						'attributes'       => $size( 'S' ),
+						'global_unique_id' => '0012345678912',
+					),
+					array(
+						'attributes'       => $size( 'M' ),
+						'global_unique_id' => '0012345678905',
+					),
+				),
+			)
+		);
+
+		$this->assertWPError( $repeating );
+		$this->assertSame( 'woocommerce_product_variation_global_unique_id_duplicate', $repeating->get_error_code() );
+
+		$mixed_case = wp_get_ability( 'woocommerce/product-create' )->execute(
+			array(
+				'name'       => 'Book',
+				'type'       => 'variable',
+				'attributes' => array(
+					array(
+						'name'    => 'Size',
+						'options' => array( 'S', 'M' ),
+					),
+				),
+				'variations' => array(
+					array(
+						'attributes'       => $size( 'S' ),
+						'global_unique_id' => '080442957X',
+					),
+					array(
+						'attributes'       => $size( 'M' ),
+						'global_unique_id' => '080442957x',
+					),
+				),
+			)
+		);
+
+		$this->assertWPError( $mixed_case, 'An ISBN-10 check digit X is the same in either case.' );
+		$this->assertSame( 'woocommerce_product_variation_global_unique_id_duplicate', $mixed_case->get_error_code() );
+	}
+
 	/**
 	 * @testdox Should accept sale dates and the tax status on external products, but not sold individually.
 	 */
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 10ecbcc9007..9dfe850724a 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/ProductsQueryTest.php
@@ -207,6 +207,28 @@ class ProductsQueryTest extends WC_Unit_Test_Case {
 		$this->assertArrayNotHasKey( 'low_stock_amount', $item );
 	}

+	/**
+	 * @testdox Should find products and variations whose GTIN equals any of the GTINs sent, and nothing for GTINs nobody has.
+	 */
+	public function test_filters_by_global_unique_id(): void {
+		$product = WC_Helper_Product::create_simple_product();
+		$product->set_global_unique_id( '4006381333931' );
+		$product->save();
+		$longer = WC_Helper_Product::create_simple_product();
+		$longer->set_global_unique_id( '40063813339310' );
+		$longer->save();
+		$variation = wc_get_product( WC_Helper_Product::create_variation_product()->get_children()[0] );
+		$variation->set_global_unique_id( '0012345678912' );
+		$variation->save();
+
+		$find = static fn( array $gtins ): array => array_column( ProductsQuery::execute( array( 'global_unique_id' => $gtins ) )['products'], 'id' );
+
+		$this->assertSame( array( $product->get_id() ), $find( array( '4006381333931' ) ), 'A GTIN that only shares a prefix should not match.' );
+		$this->assertEqualsCanonicalizing( array( $product->get_id(), $variation->get_id() ), $find( array( '4006381333931', '0012345678912' ) ) );
+		$this->assertSame( array( $variation->get_id() ), $find( array( '0012345678912', '9999999999999' ) ) );
+		$this->assertSame( array(), $find( array( '9999999999999' ) ) );
+	}
+
 	/**
 	 * @testdox Should return the exact total of matching products on partial, empty and out-of-range pages.
 	 */