Description
If you have an attribute that has a value and you import an empty string because the value needs to be empty, the attribute will not be overwritten with the empty string.
Preconditions
- MG2.1.2
- PHP7.0
Steps to reproduce
- Enter a value for an attribute for a product through the backend of Magento
(I'm using description as my example) - Create an import csv and leave the column for the attribute blank
- Import the csv with Add/Update behaviour
Expected result
- Product attribute value should be empty
Actual result
- Product attribute keeps the previous value
This issue can be traced back to \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType where the clearEmptyData function unsets all empty rows.
public function clearEmptyData(array $rowData)
{
foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
if (!$attrParams['is_static'] && empty($rowData[$attrCode])) {
unset($rowData[$attrCode]);
}
}
return $rowData;
}
It is my opinion that support for empty rows should be allowed.
My suggested fix is adding a constant to the AbstractType class and set the row values that contain that constant value to an empty string. That way if the constant value is used in the import csv, the importer will know which fields are just empty and which fields contain empty values.
CONST EMPTY_ROW_VALUE = '__EMPTY__';
public function clearEmptyData(array $rowData)
{
foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
if (!$attrParams['is_static'] && empty($rowData[$attrCode])) {
unset($rowData[$attrCode]);
continue;
}
if ($rowData[$attrCode] === self::EMPTY_ROW_VALUE) {
$rowData[$attrCode] = '';
}
}
return $rowData;
}
It would be even better to allow for setting custom values for the EMPTY_ROW_VALUE constant through the backend.