How to Add Custom Fields to a Product Feed in Magento 2

Although Magento 2 Data Feed Generator supports all product attributes, sometimes you may need to change the display of a particular attribute or add additional fields to the feed.

All fields are recorded in the feed in the {entity.property} format, which we call tags. You can read more information about them in our Data Feed Generator Tags documentation.

Let’s add a Delivery Time field to a product feed.

Step 1. Add {product.delivery_time} tag to the feed

For XML feed, add the tag to the Template Editor -> Item Element field.

<item>
    ...
    <delivery_time>{product.delivery_time}</delivery_time>
    ...
</item>

For CSV feed, add the name of the field to the Template Editor -> Header field, and the tag – to the Template Editor -> Item Element field.

Template Editor -> Header
...","delivery_time","...
Template Editor -> Item Element
...},{product.delivery_time},{...

Step 2. Create a file that will determine delivery_time for the product.

For this, you need to implement the \Plumrocket\Datagenerator\Model\Feed\Resolver\FieldResolverInterface interface.

app/code/Vendor/Module/Model/DeliveryTimeResolver.php
<?php
declare(strict_types=1);

namespace Vendor\Module\Model;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Store\Api\Data\StoreInterface;
use Plumrocket\Datagenerator\Model\Feed\Resolver\FieldResolverInterface;
use Plumrocket\Datagenerator\Model\Feed\TagInterface;

class DeliveryTimeResolver implements FieldResolverInterface
{
    public function resolve(
        TagInterface $tag,
        ProductInterface $product,
        StoreInterface $store,
        array $data,
        array $params = []
    ): string {
        return '2 working days';
    }
}

Step 3. Register the resolver for the delivery_time tag in di.xml

app/code/Vendor/Module/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Plumrocket\Datagenerator\Model\Feed\Resolver\FieldComposite">
        <arguments>
            <argument name="fieldResolvers" xsi:type="array">
                <item name="delivery_time" xsi:type="object">Vendor\Module\Model\DeliveryTimeResolver</item>
            </argument>
        </arguments>
    </type>
</config>

The Delivery Time field is now added to the product.

Was this article helpful?