0

I am trying to write custom rules for Magento 2 product import data. For example Price must contain specific number of digits based on store view. How can I do such type of validations on csv data and add the error in the error report file same as Magento way.

1 Answer 1

1

Add the following code in di.xml

<type name="Magento\CatalogImportExport\Model\Import\Product\Validator">
    <arguments>
        <argument name="validators" xsi:type="array">
            <item name="yourcustompricevalidator" xsi:type="object">VendorName\CustomModule\Model\Import\Product\Validator\PriceValidator</item>
        </argument>
    </arguments>
</type>

app/code/VendorName/CustomModule/Model/Import/Product/Validator/PriceValidator.php

<?php

namespace VendorName\CustomModule\Model\Import\Product\Validator;

use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
use Magento\CatalogImportExport\Model\Import\Product\Validator\AbstractImportValidator;

class PriceValidator extends AbstractImportValidator implements RowValidatorInterface
{
    public function isValid($value)
    {
        $this->_clearMessages();
        if (!$value['price']) {
            $this->_addMessages(
                [
                    'YOUR CUSTOM MESSAGE'
                ]
            );
            return false;
        }

        return true;
    }
}

Modify the if (!$value['price']) { logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.