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.

8 comments

  • Sam Stones

    I have a problem with my attribute values being wiped when I use the mass update attributes feature from within Magento. What happens is that when I actually click “save” it doesn’t go any further as it says I need to provide values for the required fields. These required fields aren’t actually being updated, so shouldn’t be required. If I do select these fields and provide values Magento wipes my other attributes.

    I don’t know if this is the same problem or not? but I figured I’d try your fix in case the functionality I’m using uses the same code as the import / export code, however my function looks like this:

    /**
    * Prepare attributes values for save: remove non-existent, remove empty values, remove static.
    *
    * @param array $rowData
    * @param bool $withDefaultValue
    * @return array
    */
    public function prepareAttributesForSave(array $rowData, $withDefaultValue = true)
    {
    $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 ($withDefaultValue && null !== $attrParams[‘default_value’]) {
    $resultAttrs[$attrCode] = $attrParams[‘default_value’];
    }
    }
    }
    return $resultAttrs;
    }

    As such, I’m a bit unsure on what I should be changing?

    TIA if anyone can provide any insight into this!

    • Giles (author)

      Sam,

      A bit of an odd one! If there’s an issue with the bulk updating of products through the admin panel, then I think the first step would be to look and see if there are any modifications to the core code, or any third party modules in place which could impact on that space? I’ve never come across someone having trouble the bulk attribute updating function of the admin panel, to be honest?

      The code for that, and the code for the import / export function, are separate, so it’s unlikely that the code in this post would be beneficial to you.

      Kind regards

      Giles

  • Sandro

    Hi Giles.
    This fix worked form me in one Magento instance, but now even if I try to import a simple CSV file with the sku and another Yes/No attribute nothing happens.
    It only imports textual attributes.

    Can you help me?
    Thank you.

  • Mike

    Thanks a lot, what an annoying bug this was. Now just have to work out all the attributes that got reset without me knowing about them, grrrr…

    • Giles (author)

      You’re welcome. A completely stupid bug, I agree…!

  • Stan

    Thanks for the fix!

    I was wondering if that branch statement was to do with creating new products, which it wasn’t – makes me wonder why on earth it was added on the first place!

    • Giles (author)

      No problem. Ours is not to reason why…!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to talk to us about your project?