Magento Import

Problem: Multiselect attribute options are missing during product imports in Magento:

Even in the Community Edition version 1.9.1.0, there is still a bug that prevents the smooth import of multiselect attributes. The problem: After importing products with multiselect attributes, some attribute options are missing for certain products.

Even if the various options are correctly distributed across individual lines, some options are lost for one product or another, seemingly at random or without any recognizable pattern. The cause is a bug in the import logic that discards import rows in connection with batch processing. The solution is to modify the Mage_ImportExport_Model_Import_Entity_Product class, which is defined in  \app\code\core\Mage\ImportExport\Model\Import\Entity\Product.php. After copying it to the local directory, we modify \app\code\local\Mage\ImportExport\Model\Import\Entity\Product.php as follows:

 

1) Line 1279 (based on Magento CE 1.9.1.0):

 

Old code:

$priceIsGlobal = Mage::helper('catalog')->isPriceGlobal();
$productLimit = null;
$productsQty = null;

 

New code:

$productLimit = null;
$productsQty = null;
$previousType = null;
$previousAttributeSet = null;
$previousAttributes = null;

 

2) Line 1283:

 

Old code:


while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$entityRowsIn = array();
$entityRowsUp = array();
$attributes = array();
$websites = array();
$categories = array();
$tierPrices = array();
$groupPrices = array();
$mediaGallery = array();
$uploadedGalleryFiles = array();
$previousType = null;
$previousAttributeSet = null;

 

New code:

while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$entityRowsIn = array();
$entityRowsUp = array();
$attributes = array();
$websites = array();
$categories = array();
$tierPrices = array();
$groupPrices = array();
$mediaGallery = array();
$uploadedGalleryFiles = array();
//$previousType = null;
//$previousAttributeSet = null;

 

3) Line 1413:

 

Old code:

 

try {
$attributes = $this->_prepareAttributes($rowData, $rowScope, $attributes, $rowSku, $rowStore);

 

New code:

 

try {
//$attributes = $this->_prepareAttributes($rowData, $rowScope, $attributes, $rowSku, $rowStore);
$attributes = $this->_prepareAttributes($rowData, $rowScope, $attributes, $rowSku, $rowStore, $previousAttributes);

 

4) Line 1421:

 

Old code:

 

$this->_saveProductEntity($entityRowsIn, $entityRowsUp)
->_saveProductWebsites($websites)
->_saveProductCategories($categories)
->_saveProductTierPrices($tierPrices)
->_saveProductGroupPrices($groupPrices)
->_saveMediaGallery($mediaGallery)
->_saveProductAttributes($attributes);

 

New code:

 

$this->_saveProductEntity($entityRowsIn, $entityRowsUp)
->_saveProductWebsites($websites)
->_saveProductCategories($categories)
->_saveProductTierPrices($tierPrices)
->_saveProductGroupPrices($groupPrices)
->_saveMediaGallery($mediaGallery)
->_saveProductAttributes($attributes);
$previousAttributes = $attributes;

 

5) Line 1470:

 

Old code:

 

protected function _prepareAttributes($rowData, $rowScope, $attributes, $rowSku, $rowStore)
{

 

New code:

 

//protected function _prepareAttributes($rowData, $rowScope, $attributes, $rowSku, $rowStore)
protected function _prepareAttributes($rowData, $rowScope, $attributes, $rowSku, $rowStore, $previousAttributes)
{

 

6) Line 1506:

 

Old code:

 

foreach ($storeIds as $storeId) {
if ('multiselect' == $attribute->getFrontendInput()) {
if (!isset($attributes[$attrTable][$rowSku][$attrId][$storeId])) {
$attributes[$attrTable][$rowSku][$attrId][$storeId] = '';
} else {
$attributes[$attrTable][$rowSku][$attrId][$storeId] .= ',';
}

 

New code:

 

foreach ($storeIds as $storeId) {
if ('multiselect' == $attribute->getFrontendInput()) {
if (!isset($attributes[$attrTable][$rowSku][$attrId][$storeId])
&& isset($previousAttributes[$attrTable][$rowSku][$attrId][$storeId])) {
$attributes[$attrTable][$rowSku][$attrId][$storeId] =
$previousAttributes[$attrTable][$rowSku][$attrId][$storeId].',';
}
if (!isset($attributes[$attrTable][$rowSku][$attrId][$storeId])) {
$attributes[$attrTable][$rowSku][$attrId][$storeId] = '';
} else {
$attributes[$attrTable][$rowSku][$attrId][$storeId] .= ',';
}

 

7) Done. Did I figure this out myself? No. Many, many thanks for this solution to Paul Hachmang (see the thread at https://github.com/avstudnitz/AvS_FastSimpleImport/issues/69)! Tested with Magento CE 1.9.1.0.