Magento bug – product import resets attribute values

This Magento product import error has been plaguing a couple of clients for a while now. When importing existing products via CSV to mass update them, attributes were being changed even though no changes were being specified in the CSV file. In fact, the opposite – none of the columns in the CSV related to the attributes in question, so nothing should have been changed at all.

A little digging revealed the source of the problem. In

app/code/core/Mage/ImportExport/Model/Import/Entity/Product/Type/Abstract.php

there’s a function called prepareAttributesForSave. Within this function (lines 303 – 304) there’s an elseif clause which looks like this :

} elseif (null !== $attrParams['default_value']) {
$resultAttrs[$attrCode] = $attrParams['default_value'];
}

What that’s doing is setting the attribute to its default value if it’s not contained in the CSV being processed. So resetting everything – great.
 

The Solution


To undo it, simply make a local copy at

app/code/local/Mage/ImportExport/Model/Import/Entity/Product/Type/Abstract.php

and remove the elseif clause in question, so the remainder of that function looks like this :

public function prepareAttributesForSave(array $rowData)
    {
        $resultAttrs = array();

        foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
            if (!$attrParams['is_static']) {
                if (isset($rowData[$attrCode]) && strlen($rowData[$attrCode])) {
                    $resultAttrs[$attrCode] =
                        ('select' == $attrParams['type'] || 'multiselect' == $attrParams['type'])
                        ? $attrParams['options'][strtolower($rowData[$attrCode])]
                        : $rowData[$attrCode];
                } elseif (array_key_exists($attrCode, $rowData)) {
                    $resultAttrs[$attrCode] = $rowData[$attrCode];
                }
            }
        }
        return $resultAttrs;
    }

And that’s it – if your imported CSV doesn’t mention a particular attribute, it won’t be changed back to the default.