Resolvers
This page is for the PHP developer who needs a mapping source that no attribute holds and no template can build: a value worked out per product, such as a weight with its unit or a price in another form. A resolver is that source. Once registered, it is listed in the Computed group of the mapping's Value picker, beside the module's own: Product URL, Image URL, Price with currency and the rest.
A resolver runs once per product row and field. What it returns goes through the row's dictionary and modifiers like any other value; see Where each one runs.
Prices with currency
price (PriceResolver) appears as Price with currency. It returns the regular price with the currency, before a discount. sale_price (SalePriceResolver) appears as Sale price with currency, empty when not discounted. It returns the discounted price with the currency, or an empty value when the product is not discounted.
current_price (Arlanix\ProductFeed\Model\Mapping\Resolver\CurrentPriceResolver) appears as Current price with currency. It returns the price the product sells at now, including the discounted price when discounted, with the currency: for example, 80.00 EUR. It reads final_price, falling back to minimal_price and then price when the preceding value is absent. It returns an empty string for a nonnumeric price or missing currency. If the field stays empty after the dictionary and modifiers, Required leaves the row out. A field required only by the catalog gets a warning. PriceRunner's catalog copies this resolver into its Price row.
Main and additional images
all_images (Arlanix\ProductFeed\Model\Mapping\Resolver\AllImagesResolver) appears as Main and additional images. It returns a list of URLs: the main image first when present, followed by the gallery URLs, with duplicates removed. It composes ImageResolver and AdditionalImagesResolver, declares the gallery and image attributes, and offers the same size argument, labelled Size, as additional_images. An empty size keeps the original URLs; a chosen size uses the image resolvers’ sized URLs.
To turn the list into one delimited column, see Join a list. For one XML element per URL, leave the images as a list and see Heureka XML. See European marketplaces for the copied image mappings.
Marketplace category
marketplace_category (Arlanix\ProductFeed\Model\Mapping\Resolver\MarketplaceCategoryResolver) appears as Marketplace category: the category chosen for the feed type's tree. It uses the deepest of the product's Magento categories with an assignment, including one inherited from an ancestor. At equal depth, the lowest Magento category ID wins. If there is no assignment, it uses the feed's Default category. With neither, it returns an empty string.
The Heureka, Zboží.cz and Glami catalogs use it for CATEGORYTEXT. A listed category gives its full name in the feed type's tree and Taxonomy locale, such as Heureka.cz | Auto-moto | Auta. If that tree is unavailable or lacks the chosen ID, the resolver returns an empty string. A category entered as text keeps its spelling and separators.
Write a resolver
For the Google Shopping catalog's shipping_weight field, this resolver reads general/locale/weight_unit for the row's store view and converts the stored weight to the chosen output unit (kg by default):
<?php
declare(strict_types=1);
namespace Vendor\Module\Model\Resolver;
use Arlanix\ProductFeed\Model\Mapping\MappingException;
use Arlanix\ProductFeed\Model\Mapping\Resolver\ResolverInterface;
use Arlanix\ProductFeed\Model\Mapping\Resolver\SuggestsArgumentsInterface;
use Arlanix\ProductFeed\Model\Mapping\Resolver\ValidatesArgumentsInterface;
use Arlanix\ProductFeed\Model\Mapping\RowContext;
use Magento\Directory\Helper\Data as DirectoryData;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Phrase;
use Magento\Store\Model\ScopeInterface;
class ShippingWeight implements
ResolverInterface,
SuggestsArgumentsInterface,
ValidatesArgumentsInterface
{
private const KG_PER_UNIT = ['kg' => 1.0, 'lb' => 0.45359237];
public function __construct(private readonly ScopeConfigInterface $scopeConfig)
{
}
public function resolve(RowContext $context, array $arguments = []): mixed
{
$weight = $context->getValue('weight');
$configured = $this->scopeConfig->getValue(
DirectoryData::XML_PATH_WEIGHT_UNIT,
ScopeInterface::SCOPE_STORE,
$context->getStoreId()
);
$source = ['kgs' => 'kg', 'lbs' => 'lb'][$configured] ?? null;
if (!is_numeric($weight) || $source === null) {
return null;
}
$unit = $arguments['unit'] ?? 'kg';
$converted = (float)$weight * self::KG_PER_UNIT[$source] / self::KG_PER_UNIT[$unit];
return round($converted, 8) . ' ' . $unit;
}
public function getLabel(): Phrase
{
return __('Shipping weight — the weight with its unit');
}
public function attributesFor(array $arguments): array
{
return ['weight'];
}
public function argumentSuggestions(): array
{
return [[
'name' => 'unit',
'label' => (string)__('Unit'),
'items' => [
['value' => 'kg', 'label' => 'kg'],
['value' => 'lb', 'label' => 'lb'],
],
]];
}
public function validateArguments(array $arguments): void
{
$unit = $arguments['unit'] ?? 'kg';
if (!isset(self::KG_PER_UNIT[$unit])) {
throw new MappingException(new Phrase('Unknown unit "%1".', [$unit]));
}
}
}
All the interfaces are in Arlanix\ProductFeed\Model\Mapping\Resolver. Only ResolverInterface is required:
| Method | Does |
|---|---|
resolve(RowContext $context, array $arguments = []): mixed |
Returns the field's value for one product row. Return null or '' when the product has none: if the field is still empty after the dictionary and modifiers, Required leaves the row out. $arguments holds the store's choices, keyed by the names argumentSuggestions() declares, as strings. |
getLabel(): Phrase |
The name in the Value picker, which shows it with the alias: Shipping weight — the weight with its unit (shipping_weight). |
attributesFor(array $arguments): array |
The attribute codes it reads with these arguments, such as ['weight']; [] when it reads only what every row has. See Declare the attributes it reads. |
RowContext holds the row: getValue('weight') reads a field, getValue('parent.sku') a field of a configurable parent, getProductId() and getStoreId() say whose row it is. For every method, see What a row holds.
An exception from resolve() stops the whole run. Throw only for a fault the store has to fix; for a product with no value, return null.
Declare the attributes it reads
A row is read with only the product attributes the feed's mapping and conditions name, so a run reads no more of the catalogue than it needs. Every resolver names the attributes it reads with the arguments it is given in attributesFor(), such as ['weight'].
An undeclared attribute can be present if another rule or condition of this feed requests it. getValue() returns null for a missing attribute.
These fields are in every row and need no declaration: entity_id, sku, type_id, name, status, visibility, price, final_price, minimal_price, has_discount, currency, qty, is_in_stock, stock_status and url. Declare any other product attributes your resolver needs, including the sources of computed image URLs. Declaring image_url loads the image attribute it is made from.
Offer arguments
An argument lets one resolver serve several rows: the image resolver's size, the unit of the example. With SuggestsArgumentsInterface, the mapping row shows a control for each argument:
| Method | Does |
|---|---|
argumentSuggestions(): array |
Returns one entry per argument: name, the key in $arguments; label, the control's label; items, the choices as value and label. An empty items gives a text box. |
When Copy from uses a field catalog, it leaves resolver arguments empty for the store to choose on the row. Copying a saved feed's mapping keeps its resolver arguments.
Check the arguments
With ValidatesArgumentsInterface, wrong arguments are refused when the feed is saved, and checked again before Preview and before every run:
| Method | Does |
|---|---|
validateArguments(array $arguments): void |
Throws Arlanix\ProductFeed\Model\Mapping\MappingException naming what is wrong. The store sees the message after the field's name: Field "shipping_weight": Unknown unit "oz". |
Read the feed
A resolver whose answer depends on the feed, such as the feed's own category assignments, implements FeedAwareInterface:
| Method | Does |
|---|---|
forFeed(Feed $feed): void |
Called with the feed at the start of each pass of its mapping. Read there what you need of it, and anything the store can change meanwhile, for the calls to resolve() that follow: a cron process may map for hours. |
Register it
In your module's etc/di.xml, add the resolver to Resolver\Pool under its alias:
<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\Resolver\Pool">
<arguments>
<argument name="resolvers" xsi:type="array">
<item name="shipping_weight" xsi:type="object">Vendor\Module\Model\Resolver\ShippingWeight</item>
</argument>
</arguments>
</type>
</config>
The alias is what a mapping row stores, and what a field catalog's default_resolver names; see Field catalogs. The aliases the module registers are image, additional_images, all_images, product_url, price, sale_price, current_price, sale_window, product_type, google_product_category and marketplace_category. An item under one of those names replaces the module's resolver.
Then run bin/magento setup:upgrade and bin/magento cache:clean config. Open a feed, choose Shipping weight in the Value picker of a row, choose a unit, save, and check the value with Preview.