zdenekgebauer / google-merchant-feed
Create product XML feed for Google Merchant Center
Package info
github.com/zdenekgebauer/google-merchant-feed
pkg:composer/zdenekgebauer/google-merchant-feed
Requires
- php: >=8.4
- ext-mbstring: *
- ext-xmlwriter: *
Requires (Dev)
- codeception/codeception: ^5.3
- codeception/module-asserts: ^3.3
- infection/codeception-adapter: ^0.4
- infection/infection: ^0.35
- phpstan/phpstan: ^2.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.1
- rector/rector: ^2.9
- shipmonk/composer-dependency-analyser: ^1.8
- symplify/easy-coding-standard: ^13.3
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-02 16:53:51 UTC
README
Create the product XML feed (RSS 2.0) for Google Merchant Center by the specification
https://support.google.com/merchants/answer/7052112
Requirements
- PHP 8.4 or newer
ext-mbstringandext-xmlwriter
No runtime dependencies beyond that.
Installation
composer require zdenekgebauer/google-merchant-feed
Usage
use ZdenekGebauer\GoogleMerchantFeed\Availability; use ZdenekGebauer\GoogleMerchantFeed\Condition; use ZdenekGebauer\GoogleMerchantFeed\Feed; use ZdenekGebauer\GoogleMerchantFeed\Item; use ZdenekGebauer\GoogleMerchantFeed\Price; use ZdenekGebauer\GoogleMerchantFeed\ProductDetail; use ZdenekGebauer\GoogleMerchantFeed\Shipping; use ZdenekGebauer\GoogleMerchantFeed\Weight; $feed = new Feed(); // truncates the file; items are appended to it in batches, so the whole feed never sits in memory $feed->open('/path/to/google.xml', 'My shop', 'https://example.org/', 'Product feed'); // required values $item = new Item( id: 'SKU-123', title: 'Samsung Galaxy S25 128 GB', description: 'Phone with a 6.2" display', link: 'https://example.org/samsung-galaxy-s25', imageLink: 'https://example.org/img/s25.jpg', price: new Price(19990.0, 'CZK'), ); // optional values $item->availability = Availability::IN_STOCK; $item->condition = Condition::NEW; $item->salePrice = new Price(17990.0, 'CZK'); $item->setAdditionalImageLinks(['https://example.org/img/s25-back.jpg']); $item->brand = 'Samsung'; $item->setGtins(['8806095000000']); $item->mpn = 'SM-S931B'; $item->googleProductCategory = '267'; $item->productType = 'Electronics > Mobile phones'; $item->itemGroupId = 'galaxy-s25'; $item->color = 'black'; $item->productWeight = new Weight(162, Weight::G); $item->addProductDetail(new ProductDetail('Memory', '128 GB', 'General')); $item->customLabel0 = 'new'; $shipping = new Shipping('CZ', 'Standard', new Price(99.0, 'CZK')); $shipping->minHandlingTime = 1; $shipping->maxHandlingTime = 2; $shipping->minTransitTime = 1; $shipping->maxTransitTime = 3; $item->addShipping($shipping); $feed->add($item); // ...add more items $feed->close();
Prices and tax
The feed has no tax attributes; tax is part of price and every other price
(https://support.google.com/merchants/answer/7052209):
- Czechia, the EU and most other countries — the price includes VAT.
- The US and Canada — the price excludes any tax; tax is shown at checkout.
Countries and currencies
All prices of one item — price, sale_price, shipping, returns, installments and the rest — must be in one
currency; Feed::add() throws an exception otherwise. The landing page (link) must show the same price in the same
currency as the feed.
- One feed per country and currency (recommended when the shop has its own prices per country, e.g. CZK for
Czechia and EUR for Slovakia): open a separate
Feedfor each file, write texts in the language of the country and only theShippingof that country, and add each file to Merchant Center as its own data source with that country as the target. The same itemidmay appear in several feeds. - One feed with currency conversion (a quick way to reach another country in the same language): write the feed in one currency, add more target countries to the data source and Google shows converted, approximate prices; the shipping of every country stays in the feed currency.
An item with installment may target only one country — neither its Shipping nor the target countries of the data
source may name another one. Write installments only to a feed for one country. The library cannot check this, the
target countries are set in Merchant Center.
foreach (['CZ' => 'CZK', 'SK' => 'EUR'] as $country => $currency) { $feed = new Feed(); $feed->open('/path/to/google-' . strtolower($country) . '.xml', 'My shop', 'https://example.org/', 'Products'); foreach ($products as $product) { $item = new Item(/* texts, link and price of $country in $currency */); $item->addShipping(new Shipping($country, 'Standard', new Price($shippingPrice, $currency))); $feed->add($item); } $feed->close(); }
Categories
Categories::parse() reads a downloaded Google product taxonomy file and yields the ID and full path of every
category. The library does not download the file; fetch it yourself, e.g. in a scheduled job:
https://www.google.com/basepages/producttype/taxonomy-with-ids.<locale>.txt
| Language | File |
|---|---|
| Czech | https://www.google.com/basepages/producttype/taxonomy-with-ids.cs-CZ.txt |
| English (US) | https://www.google.com/basepages/producttype/taxonomy-with-ids.en-US.txt |
Other languages follow the same pattern (de-DE, sk-SK, pl-PL, …).
All language files describe the same tree: a category has the same ID, the same parent and the same position in every language; only the texts are translated. Use the language only for what people read, and the ID for everything else:
- Store and write the numeric ID —
$item->googleProductCategory = '2591';. Google maps the ID to the category regardless of the feed language, and the ID survives a change of the shop language. - Show the category names in the language of whoever assigns categories, e.g.
cs-CZin a Czech administration. - Translations are not always exact (
3074 Executive ToysisHračky pro dospěléin Czech,3253 Officiating UniformsisSlavnostní uniformy). Showing theen-USname next to the translated one avoids a wrong choice; both files yield the same keys:
use ZdenekGebauer\GoogleMerchantFeed\Categories; $english = iterator_to_array(new Categories()->parse('/path/to/taxonomy-with-ids.en-US.txt')); foreach (new Categories()->parse('/path/to/taxonomy-with-ids.cs-CZ.txt') as $id => $path) { // 2591 => 'Kancelářské potřeby > … > Opravné laky, pera a pásky' (Office Supplies > … > Correction Fluids, …) $label = $path . ' (' . $english[$id] . ')'; }
Notes on the files:
- The first line is a comment with the taxonomy version (
# Google_Product_Taxonomy_Version: 2021-09-21); compare it after a download to detect a new taxonomy. - Lines are sorted alphabetically by the translated path, so the order differs between languages.
- The Czech file contains a few no-break spaces (U+00A0) inside or at the end of names; they are kept in the path.
Errors
Feed::add() validates the item before it writes anything and throws InvalidArgumentException when
id,title,descriptionorlinkis emptypriceis not greater than zero or is not finitelink,image_link, an additional image,video_linkor a document link does not start withhttp://orhttps://, or contains whitespace, control or non-ASCII characters (percent-encode each path segment, e.g. withrawurlencode())- a list exceeds its limit: 10 additional images, 5 document links, 100 product details, 30 variant options, related products and questions, 2 size types and size systems
- any other price of the item is in another currency than
price - the item is a preorder without
availability_date - a product dimension is outside 1–3000, the product weight outside 0–2000 or
popularity_rankoutside 0–100 unit_pricing_base_measureis not one Google supports (1, 2, 4, 8, 10, 100 of a unit,75cl,750ml,50kg,1000kg)- only some of the shipping dimensions are set, or they mix units
- a list of business days is empty or names a day twice
- a shipping group sets more than one of
region,postal_code,location_idandlocation_group_name - a return policy has no method, has a finite return window without
window_days, or sets both restocking fees
Further InvalidArgumentExceptions:
| Thrown by | When |
|---|---|
UnitMeasure::__construct() |
the quantity is not greater than zero or is not finite |
Item::setProductHighlights() |
1 or more than 100 highlights (zero or 2–100 are allowed) |
Item::setPromotionIds() |
more than 10 promotion IDs |
Feed::add() and Feed::close() throw LogicException when open() was not called. Categories::parse() throws
RuntimeException when the file cannot be opened or contains an invalid line.
Output notes
- Values over the length limits of the specification are truncated silently, by characters — e.g.
id50,title150,description5000,product_type750,brandandmpn70,custom_label_*100 - HTML tags are stripped from
description - Free text (title, description, brand, labels, variant names, questions and answers, …) is written as CDATA
- Optional elements are omitted when
null; handling times, transit times andmultipackalso when not positive - Booleans are written as
yes/no, prices with two decimal places (1234.50 CZK)
Known Issues
- Values are not checked against the specification beyond the rules above — e.g. the GTIN check digit, country or currency codes, or the target countries of the data source
- Only UTF-8 output
Not implemented
These attributes of the specification are not written by the library:
structured_title,structured_description— usetitleanddescriptionhandling_cutoff_timevirtual_model_link— US onlymaximum_retail_price— India onlycarrier_shipping— Google calculates carrier rates only for Australia, France, Germany, the UK and the US
License
MIT, see LICENSE