Row transformers
This page is for the PHP developer whose feed needs what the mapping cannot express: a price your ERP decides, a product one market must not see, one row per pack size. A row transformer is your own code over a feed's rows. It runs between the product as it is read and the mapping, so every channel, file, SFTP, Merchant API and Preview, sees what it returns. For the other ways to extend a feed, see Extend the module.
Write one
Implement Arlanix\ProductFeed\Model\Mapping\RowTransformerInterface. Each pack inherits the unit price unless you override its price fields. This example uses fixed pack prices in base currency and passes them to RowPrice::fields() when it builds the variants:
<?php
declare(strict_types=1);
namespace Vendor\Module\Model;
use Arlanix\ProductFeed\Model\Catalog\Price\RowPrice;
use Arlanix\ProductFeed\Model\Feed;
use Arlanix\ProductFeed\Model\Mapping\RowContext;
use Arlanix\ProductFeed\Model\Mapping\RowTransformerInterface;
class PackSizes implements RowTransformerInterface
{
public function __construct(private readonly RowPrice $rowPrice)
{
}
public function appliesTo(Feed $feed): bool
{
return $feed->getType() === 'google_shopping';
}
public function prepare(Feed $feed, array $rows): void
{
// Read any extra product data for this page in bulk here.
}
public function transform(Feed $feed, RowContext $row): array
{
if ($row->getValue('sku') === 'DISCONTINUED') {
return []; // left out
}
$packs = [];
foreach ([6 => 50.0, 12 => 95.0] as $size => $packPrice) {
$packs[] = $row->withVariant((string)$size, [
'sku' => $row->getValue('sku') . '-' . $size,
'name' => $row->getValue('name') . ', pack of ' . $size,
] + $this->rowPrice->fields($feed, $row, $packPrice));
}
return $packs;
}
}
transform() answers with the rows of the product it was given:
| It returns | What happens |
|---|---|
[$row->with([...])] |
The row goes on with those fields overwritten. |
[] |
Drops this row. The product is left out if none of its rows remain. For push channel removals, see What the interface asks. |
[$row->withVariant('a', [...]), $row->withVariant('b', [...])] |
One row per variant, each delivered and taken down on its own. A variant of a variant keeps both keys: a-x. A key is 1 to 64 characters, both keys together included; an empty or longer one throws InvalidArgumentException. For a push channel, keep the variant key within 255 bytes as well. |
Each row arrives with its prices, stock and addresses already in it (price, final_price, special_price, currency, url, …), so overwriting one of them is all it takes to change what the mapping reads. The prices are in the feed's currency and tax mode. A price of your own goes in through Arlanix\ProductFeed\Model\Catalog\Price\RowPrice, given in base currency as the catalogue's prices are entered, with tax when Catalog Prices include it, and converted as the module converts the product's own:
fields(Feed $feed, RowContext $row, float $price, ?float $selling = null): arraygives every price field of the row, forwith()orwithVariant():$row->withVariant('6', $this->rowPrice->fields($feed, $row, 6 * $unit)). The price fields read the selling price before the price, so a row with onlypriceoverwritten would still send the product's own. Give$sellingwhen the row is on sale.fromBase(Feed $feed, RowContext $row, ?float $baseAmount): ?floatconverts one amount for a field of your own.
A row of another product, or the same variant of a product twice, stops the run with a LogicException naming the transformer.
What a row holds
transform() and prepare() are given Arlanix\ProductFeed\Model\Mapping\RowContext objects, one per product row. A row holds the fields the module computes and the attributes this feed's mapping, resolvers and conditions read. For a configurable child whose parent is enabled and visible in the store view, parent holds the parent's fields. For the attributes a resolver needs, see Declare the attributes it reads.
| Method | Returns |
|---|---|
getValue(string $path): mixed |
A field's value, or null when the row has none. A dot reads into a nested field: parent.sku. |
with(array $overrides): self |
The same row with these fields overwritten. A key with a dot is a field of that name, not a path. |
withVariant(string $key, array $overrides = []): self |
One of several rows of the product, with these fields overwritten; see Write one. |
getVariantKey(): string |
The row's variant key, empty for a product's only row. |
getProductId(): int |
The product's id. |
getStoreId(): int |
The store view the row is for. |
getData(): array |
The row's mapping values, as an array. |
getColumns(): array |
The columns kept beside the mapping values, including the tax class and visibility used for prices and URLs. |
A RowContext never changes: with() and withVariant() return a new row and leave the one they are called on as it was.
Register it
In your module's etc/di.xml, add the transformer to RowTransformerPool. sortOrder decides the order when there are several, each running on what the last one returned:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Arlanix\ProductFeed\Model\Mapping\RowTransformerPool">
<arguments>
<argument name="transformers" xsi:type="array">
<item name="pack_sizes" xsi:type="array">
<item name="class" xsi:type="object">Vendor\Module\Model\PackSizes</item>
<item name="sortOrder" xsi:type="number">10</item>
</item>
</argument>
</arguments>
</type>
</config>
An item may also be the object itself, with no sortOrder; it then counts as 0, and runs before every transformer with a positive one.
If an item does not implement the interface, the pool constructor throws Magento\Framework\Exception\LocalizedException and names the item.
What the store sees
A product a transformer leaves out is counted in the grid's Health like one the mapping leaves out. Details says a module of this installation left it out, and the feed's products list gives the reason as Excluded by row transformer "pack_sizes"., so the item name is the one to choose with the store in mind.