# EU product identifiers for the WooCommerce plugin

**Ticket:** [PMSG-285](https://p2g.atlassian.net/browse/PMSG-285)
**Status:** Phases 1 and 2 released in 2.2.0 (PR #19, `feature/pmsg-285-eu-product-identifiers`). Phases 3 (bulk
list and links) and 4 (helpers and telemetry) are **not built**. Investigation done 2026-10-22; doc checked against
the code on 2026-11-25 (2.5.0).
**References:** the Shopify app plan and implementation (`shopify-app`, PMSG-275) and ui-checkout
`docs/designs/product-identifiers.md`. This plan reuses their rules and logic.

From 1 Nov 2026 EU customs needs three identifiers per item on a distance sale to a consumer (UPS enforces from
25 Oct): **Item SKU** (M-PID, mandatory), **Manufacturer ID** (NS-PID, mandatory) and **Barcode** (S-PID, only if one
exists). The Parcel2Go requirements API decides whether they apply and are required. **The plugin follows it and
holds no scope or cap logic of its own** (same decision as the Shopify app).

## How the plugin works today (what matters here)

- PHP builds each order for the UI: `OrderFormatter::get_item_base_fields` returns `tariff_code` and
  `country_of_manufacture` from the product's `_p2g_*` meta (a variation falls back to the parent for each when its
  own is empty) and `product_identifiers` from `ProductIdentifiers::for_product`.
- The React app builds the checkout payload. `orderToCheckoutValidatePayload.ts` (single) and
  `ordersToCheckoutValidatePayload.ts` (bulk) each build `parcels[].contents[]` themselves, one content line per order
  item (virtual items included; only the parcel value leaves them out).
- The shared `ParcelBuilder` (added in 2.5.0 for checkout rates) builds the parcel size and weight for orders and
  carts. It does not touch the content lines or the identifiers.
- PHP proxies `/checkout` and `/checkout/validate` (`CheckoutController`, `P2GApiClient`). `UnitNormalizer` only
  converts parcel and content weights and sizes; other content keys, the identifiers included, pass through unchanged.
- Customs readiness before a service is chosen is still a local check (`validateInternationalRequirements`: tariff
  code and country of manufacture only), shown as a prerequisites notice. The identifiers are checked against the
  requirements API once a service is chosen (single order only).
- Products get customs data through plugin fields in the product's Shipping tab (`ProductCustomsFields.php`).
- IOSS eligibility comes from the lane's customs `ioss` rule (`isIossEligible`: parcel value at or under its
  `maxParcelValue`). The old flat `<= 150` is gone from the payload builder.

## Where the identifiers come from

| Identifier | Source | Notes |
| --- | --- | --- |
| Item SKU | Product or variation SKU (`get_sku( 'edit' )`) | A variation uses its own SKU only; the parent's is **not** inherited |
| Barcode | WooCommerce's native GTIN/UPC/EAN/ISBN field (`get_global_unique_id( 'edit' )`, WooCommerce 9.2+; falls back to the `_global_unique_id` meta on older versions) | Free text, so it is only used if it is a valid GTIN (8, 12, 13 or 14 digits with a correct check digit; ISBN-13 included). Spaces and hyphens are stripped. Never falls back to the parent |
| Manufacturer ID | Plugin field, meta `_p2g_manufacturer_product_id` (no native WooCommerce field) | Product Shipping tab. A variation without one uses the parent's |

Values are sent only if usable: trimmed, not empty, within the column width (64 / 32 / 32 characters) and without `|`
or `:`. Otherwise the value is left empty so the merchant is asked; a value is never inferred.

## Design

1. **Fields (PHP). Built.** A Manufacturer ID field beside tariff code and country of manufacture in
   `ProductCustomsFields` (product level only; no field on each variation). Up to 32 characters (`maxlength`), and on
   save a value over 32 characters or containing `|` or `:` is rejected with an error notice. The meta is removed on
   uninstall. `includes/Helpers/ProductIdentifiers.php` (`for_product`, `pick`, `is_valid_gtin`) returns the three
   values; `OrderFormatter` adds them to each item as `product_identifiers` (`merchant_product_id`,
   `manufacturer_product_id`, `standard_product_id`, `''` for none).
2. **Requirements (PHP + React). Built.** `RequirementsController`:
   `GET /parcel2go-shipping/v1/requirements?service=&origin=&destination=` (needs `manage_woocommerce`; slug and
   country codes validated) proxies `GET /services/{slug}/{origin}/{destination}/requirements` through `P2GApiClient`
   and returns `ruleSets` and `metadata`, cached in a transient for 10 minutes. The `useServiceRequirements` hook
   fetches it when a service is chosen (origin: store country, destination: shipping country) and ignores stale
   responses. A failed fetch shows no codes card and hides the IOSS box; the server's validation still decides.
3. **Shared TS logic. Built.** Ported from the Shopify app as `src/shared/utils/requirementRules.ts` (`requiredIf`
   evaluation, `shipmentRuleData`, `isIossEligible`) and `productIdentifierLines.ts` (lines to show, placeholder and
   `|` `:` checks, set one value, field labels). Shopify's `instance.ts` (error line matching) was not ported. The two
   repos hold copies.
4. **Payload. Built.** `CheckoutParcelContent` has `merchantProductId`, `manufacturerProductId`, `standardProductId`.
   `productIdentifierFields(item)` (`src/shared/utils/productIdentifiers.ts`) is spread into each content line in both
   builders and leaves out empty values. The duplicated builder loops were not refactored.
5. **Single order. Built.** After a service is chosen, the requirements' `parcelContent` rules are evaluated against
   the shipment as it is sent (parcel value in minor units, export reason Sale, duty terms and IOSS). The "Product
   codes for EU customs" card lists each item's fields, prefilled from the product, only when an identifier rule is
   required and the parcel is not over the rule's `maxParcelValue`. Edits apply to this booking only and are not saved
   to the product (the card says so). Booking is blocked while a required code is empty or a placeholder (`none`,
   `n/a`...), or any code contains `|` or `:`. An edit is sent for revalidation when the field loses focus. When the
   codes are needed only because of DDP or IOSS, the card sits inside that section and its edits apply only once the
   DDP or IOSS form is confirmed.
6. **Bulk. Partly built.** The bulk payload carries the codes, and API errors name the field. **Not built:** the
   per-order and per-product list with product edit links, and a "couldn't check this order" state.
7. **Errors. Built.** `checkout-error-notice.tsx` labels the three fields (Item SKU, Manufacturer ID, Barcode). In the
   single-order booking, an error on a field the card shows points at "product codes above" instead of an order link.
8. **Housekeeping. Done.** Translations through `__()`, readme (Set up your products, changelog), version 2.2.0, Jest
   tests in `src/__tests__`.

## Tests

- Jest (`wp-scripts test-unit-js`): `productIdentifiers.test.ts` (payload helper and both builders),
  `productIdentifierLines.test.ts`, `productIdentifierRule.test.ts`, `requirementRules.test.ts`,
  `productIdentifiersSection.test.tsx`, `checkoutErrorNotice.test.tsx`.
- PHPUnit (`tests/`): `ProductIdentifiersTest.php` (`pick` and `is_valid_gtin`: GTIN lengths and check digit,
  trimming, delimiters, widths, multibyte), `RequirementsControllerTest.php`. There is no PHPUnit test for
  `for_product` (SKU and parent fallback, missing native field on old WooCommerce) or for `UnitNormalizer` keeping
  the identifier keys.
- Manual matrix on a test store: simple product with all three; SKU only; variation inheriting from parent; order
  over the cap; non-EU; gift-like (not possible, reason is always Sale); bulk with a missing code.

## Phases

1. Data: PHP helper, product field, formatter, TS types, shared payload helper. **Released (2.2.0).**
2. Requirements route and the single-order card with booking gate, plus the IOSS cap from the API rule.
   **Released (2.2.0).**
3. Bulk: per-order and per-product list with product edit links, failed-validation state. **Not built** (only the
   payload is done).
4. Helpers ("Create one for me", "I made this item myself", help) and telemetry, after the carrier decision on
   generated codes (same blocker as Shopify). **Not built.**

## Open questions

1. **Manufacturer ID source.** Decided: a plugin field in the Shipping tab. Third-party plugin MPN meta is not read
   (as in worker-marketplaces).
2. **Variation-level Manufacturer ID.** Still open. Today it is product level with parent fallback; a variable product
   with different manufacturer codes per variation has no field per variation.
3. **Native getter.** Resolved: `get_global_unique_id( 'edit' )` on WooCommerce 9.2+, else the `_global_unique_id`
   meta. A variation uses its own value only.
4. **Country format** for the requirements path: the app sends the store country and the order's shipping country as
   WooCommerce holds them; the route accepts 2 or 3 letter codes. Not confirmed against the API for every lane.
5. **Edit-in-booking versus product-only.** Edits apply to this booking only. No "save to product" option.
6. **Carrier acceptance of generated codes** (blocks Phase 4), and the ISBN confirmation, as on Shopify.
7. The mismatched generators between ui-myaccount and ui-checkout still need resolving before Phase 4.
