HummingbirdUK main logo

HummingbirdUK

Coding solutions to business problems

About us

We use code to create solutions to business challenges, bottle-necks and headaches.

If you think your business has a problem that can be solved through code, we are happy to chat things through without any obligation.

Get in touch

Magento bug - product import resets attribute values

Home / Blog / Magento bug - product import resets attribute values

Written by Giles Bennett

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 [php]app/code/core/Mage/ImportExport/Model/Import/Entity/Product/Type/Abstract.php[/php] there's a function called prepareAttributesForSave. Within this function (lines 303 - 304) there's an elseif clause which looks like this : [php]} elseif (null !== $attrParams['default_value']) { $resultAttrs[$attrCode] = $attrParams['default_value']; }[/php] 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 [php]app/code/local/Mage/ImportExport/Model/Import/Entity/Product/Type/Abstract.php[/php] and remove the elseif clause in question, so the remainder of that function looks like this : [php]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; } [/php] And that's it - if your imported CSV doesn't mention a particular attribute, it won't be changed back to the default.
Author : Giles Bennett

About the author

Giles Bennett built his first website in 1996, and is old enough to miss Netscape Navigator. Initially a lawyer, he jumped ship to IT in 2008, and after 5 years as a freelancer, he founded HummingbirdUK in 2013. He can be reached by email at giles@hummingbirduk.com.