Search by

Validate GTIN, UPC and EAN barcodes: length, GS1 mod-10 check digit and restricted in-store prefixes. No dependencies.

v1.0.0 2026-11-09 05:31 UTC

This package is not auto-updated.

Last update: 2026-11-10 04:06:41 UTC


README

Validate GTIN, UPC and EAN barcodes in PHP before they go into a Google Shopping, Meta or marketplace product feed. One class, no dependencies.

composer require feedshine/gtin
use FeedShine\Gtin\Gtin;

Gtin::validate('4006381333931');
// ['valid' => true, 'type' => 'GTIN-13 (EAN-13)', 'expected' => 1, 'restricted' => false, 'reason' => 'Valid', ...]

Gtin::validate('4006381333932')['reason'];
// 'Check digit is 2, should be 1'

Gtin::isFeedSafe('2001234567893');
// false: the checksum passes, but 200-299 is a restricted in-store prefix

Gtin::checkDigit('400638133393');
// 1

What it checks

CheckExample that fails
Only digits (spaces and dashes are stripped)ABC123
Length is 8, 12, 13 or 1412345
Not all zeros0000000000000
GS1 mod-10 check digit4006381333932
Restricted prefixes 020-029, 040-049, 200-299 (and GTIN-8 starting 0 or 2)2001234567893

validate() returns the reason as plain English, so you can show it to whoever maintains the catalogue.

Typical uses

  • Clean a CSV export from WooCommerce, Magento or a supplier before building a feed
  • Validate the barcode field on product save
  • Flag variants that Google Merchant Center would disapprove for an invalid GTIN

Tests

php tests/run.php

The same rules, in JavaScript and as a browser tool: gtin-check on GitLab, Barcode Feed Checker, and a guide to how GTIN check digits work.

Licence and credits

MIT. Written with AI assistance by FeedShine, a Shopify app that builds Google Shopping, Meta and TikTok product feeds and runs this check on every product.