Skip to content

CatalogImportExport multiselect values will fail validation with custom multiValueSeperator #6984

Closed
@koenner01

Description

@koenner01

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

  1. Magento 2.1
  2. PHP7.0

Steps to reproduce

  1. Create a catalog_product csv that contains product data and at least 1 multiselect attribute with 2 options (and separate them with custom separator)
  2. Go to the MG backend Import interface
  3. Set your custom delimiter as 'Multiple value separator' (example separator '|||')
  4. Choose your file and click the 'Check Data' button

Expected result

  1. Validation should pass without problems

Actual result

  1. 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]);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions