Commit 3fd09fb0a0e for woocommerce
commit 3fd09fb0a0e529c75d64528323dbffd72ddc89dd
Author: AnnaMag <AnnaMag@users.noreply.github.com>
Date: Thu Nov 19 14:40:27 2026 +0100
Store API: return the product GTIN (#71104)
* Store API: return the product GTIN
Product responses gain global_unique_id (GTIN, UPC, EAN or ISBN), next to
sku. Variations return their own through /products/{variation_id}, never
the parent's. Empty when none is set.
Agents and the outside agent readiness scan read products through the Store
API. Without the field they can only read GTINs from a page's structured
data, where an invalid GTIN is left out, so it can't be told from a
missing one.
Additive: no existing field changes. The TypeScript type marks it optional,
since stores before this version don't return it.
* Fix Store API GTIN texturization of consecutive hyphens
Return global_unique_id as entered so wptexturize cannot turn -- into an en dash entity.
diff --git a/docs/apis/store-api/resources-endpoints/products.md b/docs/apis/store-api/resources-endpoints/products.md
index f621e12b8da..2b42466524e 100644
--- a/docs/apis/store-api/resources-endpoints/products.md
+++ b/docs/apis/store-api/resources-endpoints/products.md
@@ -102,6 +102,7 @@ curl "https://example-store.com/wp-json/wc/store/v1/products"
"variation": "",
"permalink": "https://local.wordpress.test/product/wordpress-pennant/",
"sku": "wp-pennant",
+ "global_unique_id": "",
"summary": "<p>This is an external product.</p>",
"short_description": "<p>This is an external product.</p>",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>",
@@ -171,6 +172,7 @@ curl "https://example-store.com/wp-json/wc/store/v1/products/34"
"variation": "",
"permalink": "https://local.wordpress.test/product/wordpress-pennant/",
"sku": "wp-pennant",
+ "global_unique_id": "",
"summary": "<p>This is an external product.</p>",
"short_description": "<p>This is an external product.</p>",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>",
@@ -239,6 +241,7 @@ curl "https://example-store.com/wp-json/wc/store/v1/products/wordpress-pennant"
"variation": "",
"permalink": "https://local.wordpress.test/product/wordpress-pennant/",
"sku": "wp-pennant",
+ "global_unique_id": "",
"summary": "<p>This is an external product.</p>",
"short_description": "<p>This is an external product.</p>",
"description": "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>",
diff --git a/plugins/woocommerce/changelog/add-store-api-product-gtin b/plugins/woocommerce/changelog/add-store-api-product-gtin
new file mode 100644
index 00000000000..3abac36bc72
--- /dev/null
+++ b/plugins/woocommerce/changelog/add-store-api-product-gtin
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Store API: return the product GTIN (UPC, EAN or ISBN) as `global_unique_id` in product responses.
diff --git a/plugins/woocommerce/client/blocks/packages/public-api/types/type-defs/product-response.ts b/plugins/woocommerce/client/blocks/packages/public-api/types/type-defs/product-response.ts
index 2847d20d4d8..68fc609f109 100644
--- a/plugins/woocommerce/client/blocks/packages/public-api/types/type-defs/product-response.ts
+++ b/plugins/woocommerce/client/blocks/packages/public-api/types/type-defs/product-response.ts
@@ -76,6 +76,7 @@ export interface ProductResponseItem {
variation: string;
permalink: string;
sku: string;
+ global_unique_id?: string;
short_description: string;
description: string;
on_sale: boolean;
diff --git a/plugins/woocommerce/src/StoreApi/Schemas/V1/ProductSchema.php b/plugins/woocommerce/src/StoreApi/Schemas/V1/ProductSchema.php
index 85a24f7de13..c1ed5fa6d15 100644
--- a/plugins/woocommerce/src/StoreApi/Schemas/V1/ProductSchema.php
+++ b/plugins/woocommerce/src/StoreApi/Schemas/V1/ProductSchema.php
@@ -115,6 +115,12 @@ class ProductSchema extends AbstractSchema {
'type' => 'string',
'context' => [ 'view', 'edit', 'embed' ],
],
+ 'global_unique_id' => [
+ 'description' => __( 'GTIN, UPC, EAN or ISBN, as entered for the product or variation. Empty when none is set.', 'woocommerce' ),
+ 'type' => 'string',
+ 'context' => [ 'view', 'edit', 'embed' ],
+ 'readonly' => true,
+ ],
'prices' => [
'description' => __( 'Price data provided using the smallest unit of the currency.', 'woocommerce' ),
'type' => 'object',
@@ -622,6 +628,7 @@ class ProductSchema extends AbstractSchema {
'variation' => $this->prepare_html_response( $product->is_type( ProductType::VARIATION ) ? wc_get_formatted_variation( $product, true, true, false ) : '' ),
'permalink' => $product->get_permalink(),
'sku' => $this->prepare_html_response( $product->get_sku() ),
+ 'global_unique_id' => (string) $product->get_global_unique_id(),
'short_description' => $short_description,
'description' => $description,
'on_sale' => $product->is_on_sale(),
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Products.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Products.php
index f8e3d037969..91c1bbc2450 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Products.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Products.php
@@ -87,6 +87,36 @@ class Products extends ControllerTestCase {
);
}
+ /**
+ * @testdox A product and each variation return their own GTIN; a variation without one doesn't get its parent's.
+ */
+ public function test_get_item_returns_gtin() {
+ $product = $this->products[1];
+ $product->set_global_unique_id( '4006381333931' );
+ $product->save();
+ $variable = \WC_Helper_Product::create_variation_product();
+ $children = $variable->get_children();
+ $variable->set_global_unique_id( '9780306406157' );
+ $variable->save();
+ $variation = wc_get_product( $children[0] );
+ $variation->set_global_unique_id( '012345678905' );
+ $variation->save();
+ $get = static fn( int $id ): array => rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/v1/products/' . $id ) )->get_data();
+
+ $this->assertSame( '4006381333931', $get( $product->get_id() )['global_unique_id'] );
+ $this->assertSame( '9780306406157', $get( $variable->get_id() )['global_unique_id'] );
+ $this->assertSame( '012345678905', $get( $variation->get_id() )['global_unique_id'] );
+ $this->assertSame( '', $get( $children[1] )['global_unique_id'] );
+
+ $product->set_global_unique_id( '123--456' );
+ $product->save();
+ $this->assertSame( '123--456', $get( $product->get_id() )['global_unique_id'], 'Consecutive hyphens stay as entered' );
+
+ $product->set_global_unique_id( '' );
+ $product->save();
+ $variable->delete( true );
+ }
+
/**
* Test getting item.
*/
@@ -105,6 +135,7 @@ class Products extends ControllerTestCase {
$this->assertEquals( $this->products[0]->get_slug(), $data['slug'] );
$this->assertEquals( $this->products[0]->get_permalink(), $data['permalink'] );
$this->assertEquals( $this->products[0]->get_sku(), $data['sku'] );
+ $this->assertSame( '', $data['global_unique_id'], 'No GTIN set: an empty string' );
$this->assertEquals( $this->products[0]->get_price(), $data['prices']->price / ( 10 ** $data['prices']->currency_minor_unit ) );
$this->assertEquals( $this->products[0]->get_average_rating(), $data['average_rating'] );
$this->assertEquals( $this->products[0]->get_review_count(), $data['review_count'] );
@@ -209,6 +240,7 @@ class Products extends ControllerTestCase {
$this->assertArrayHasKey( 'description', $data[0] );
$this->assertArrayHasKey( 'on_sale', $data[0] );
$this->assertArrayHasKey( 'sku', $data[0] );
+ $this->assertArrayHasKey( 'global_unique_id', $data[0] );
$this->assertArrayHasKey( 'prices', $data[0] );
$this->assertArrayHasKey( 'average_rating', $data[0] );
$this->assertArrayHasKey( 'review_count', $data[0] );