Import Variants using Data definitions bundle

Hello,

I’m fairly new to Pimcore.
I’m using Data Definitions to import data from a csv file.
I managed to import data, but I’m missing the product variants.
As a key I use the product name. Some products have variants so they share the same product name but have different product codes.
When I import the csv the system overwrites all the products with the same product name so it actually imports only one product per product name.

Is there a way to import products with variants? (I still want the key to be the product name)

I saw that the built-in pimcore import csv has the option of importing variants. Is there a way to indicate that also with the Data definitions bundle?

Thank you.

Marie

That is quite difficult, it heavily depends on how your import source looks like, here is an example of one of my implementations where the CSV file contains a style-code that defines the product “groups”:

<?php

namespace AppBundle\DataDefinitions\Interpreter;

use Wvision\Bundle\DataDefinitionsBundle\Interpreter\InterpreterInterface;
use Wvision\Bundle\DataDefinitionsBundle\Model\DataDefinitionInterface;
use Wvision\Bundle\DataDefinitionsBundle\Model\MappingInterface;
use League\Csv\Reader;
use League\Csv\Statement;
use Pimcore\Model\DataObject\Concrete;

class VariantInterpreter implements InterpreterInterface
{
    protected $styleMasterItemCache = [];

    protected $fullDataSet = null;

    public function interpret(Concrete $object, $value, MappingInterface $map, $data, DataDefinitionInterface $definition, $params, $configuration)
    {
       //Get the whole data-set (in this case, we import by batches, since the import source is quite big)
        if (null === $this->fullDataSet) {
            $file = sprintf('%s/%s', PIMCORE_PROJECT_ROOT, $params['file']);

            $csv = Reader::createFromPath($file, 'r');
            $csv->setDelimiter($definition->getConfiguration()['delimiter']);
            $csv->setEnclosure($definition->getConfiguration()['enclosure']);

            $csv->setHeaderOffset(0);
            $stmt = new Statement();
            $this->fullDataSet = iterator_to_array($stmt->process($csv));
        }
       //find products within the same style code
        $sameStyleCode = array_values(array_filter($this->fullDataSet, static function($dataSetEntry) use($data) {
            return ($dataSetEntry['StyleCode'] === $data['StyleCode']);
        }));

        // if only one found, there is no variant, return path or object-type object, depending on the configuration
        if (count($sameStyleCode) === 1) {
            return $map->getToColumn() === 'o_parent' ? $object->getParent() : Concrete::OBJECT_TYPE_OBJECT;
        }

     // if first found is current object, it is the "variant-master", therefore not a variant but the object itself
        if ($sameStyleCode[0]['SKU'] === $data['SKU']) {
            $this->styleMasterItemCache[$sameStyleCode[0]['SKU']] = $object;

            return $map->getToColumn() === 'o_parent' ? $object->getParent() : Concrete::OBJECT_TYPE_OBJECT;
        }

        $parent = null;

        if (array_key_exists($sameStyleCode[0]['SKU'], $this->styleMasterItemCache)) {
            $parent = $this->styleMasterItemCache[$sameStyleCode[0]['SKU']];
        }
        else {
            $parent = $object::getBySupplierItemNumber($sameStyleCode[0]['SKU'], ['limit' => 1]);
        }

        //Fallback: if not found in Pimcore
        if (!$parent instanceof $object) {
            return $map->getToColumn() === 'o_parent' ? $object->getParent() : Concrete::OBJECT_TYPE_OBJECT;
        }
        // if found return path or object-type
        return $map->getToColumn() === 'o_parent' ? $parent : Concrete::OBJECT_TYPE_VARIANT;
    }
}

Use that interpreter for o_type and o_parent within the mapping.