Closed
Description
If you are trying to import product data with the CatalogImportExport interface, validation on multiselect attributes will fail if you are using a multi value separator different from '|'.
Preconditions
- Magento 2.1
- PHP7.0
Steps to reproduce
- Create a catalog_product csv that contains product data and at least 1 multiselect attribute with 2 options (and separate them with custom separator)
- Go to the MG backend Import interface
- Set your custom delimiter as 'Multiple value separator' (example separator '|||')
- Choose your file and click the 'Check Data' button
Expected result
- Validation should pass without problems
Actual result
- Validation fails and tells us we have incorrect values for our multiselect attribute
I have traced this problem back to \Magento\CatalogImportExport\Model\Import\Product\Validator. In the function 'isAttributeValid' the following code is present:
case 'multiselect':
$values = explode(Product::PSEUDO_MULTI_LINE_SEPARATOR, $rowData[$attrCode]);
$valid = true;
foreach ($values as $value) {
$valid = $valid && isset($attrParams['options'][strtolower($value)]);
}
if (!$valid) {
$this->_addMessages(
[
sprintf(
$this->context->retrieveMessageTemplate(
RowValidatorInterface::ERROR_INVALID_ATTRIBUTE_OPTION
),
$attrCode
)
]
);
}
break;
As you can see the values are being seperated by a hardcoded constant defined in \Magento\CatalogImportExport\Model\Import\Product which is '|'. So if your separator for multiselect values is different from the hardcoded value, validation will fail (even though you set the correct value in 'Multiple value separator')
The hardcoded reference should be changed to something like
$parameters = $this->context->getParameters();
$separator = (isset($parameters[\Magento\ImportExport\Model\Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR])
? $parameters[\Magento\ImportExport\Model\Import::FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR]
: Product::PSEUDO_MULTI_LINE_SEPARATOR)
;
$values = explode($separator, $rowData[$attrCode]);