public function saveRow(array $importData) { $product = $this->getProductModel(); $product->setData(array()); if ($stockItem = $product->getStockItem()) { $stockItem->setData(array()); } if (empty($importData['store'])) { if (!is_null($this->getBatchParams('store'))) { $store = $this->getStoreById($this->getBatchParams('store')); } else { $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'store'); Mage::throwException($message); } } else { $store = $this->getStoreByCode($importData['store']); } if ($store === false) { $message = Mage::helper('catalog')->__('Skip import row, store "%s" field not exists', $importData['store']); Mage::throwException($message); } if (empty($importData['sku'])) { $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'sku'); Mage::throwException($message); } $product->setStoreId($store->getId()); $productId = $product->getIdBySku($importData['sku']); if ($productId) { $product->load($productId); } $productTypes = $this->getProductTypes(); $productAttributeSets = $this->getProductAttributeSets(); /** * Check product define type */ if (empty($importData['type']) || !isset($productTypes[strtolower($importData['type'])])) { $value = isset($importData['type']) ? $importData['type'] : ''; $message = Mage::helper('catalog')->__('Skip import row, is not valid value "%s" for field "%s"', $value, 'type'); Mage::throwException($message); } $product->setTypeId($productTypes[strtolower($importData['type'])]); /** * Check product define attribute set */ if (empty($importData['attribute_set']) || !isset($productAttributeSets[$importData['attribute_set']])) { $value = isset($importData['attribute_set']) ? $importData['attribute_set'] : ''; $message = Mage::helper('catalog')->__('Skip import row, is not valid value "%s" for field "%s"', $value, 'attribute_set'); Mage::throwException($message); } $product->setAttributeSetId($productAttributeSets[$importData['attribute_set']]); foreach ($this->_requiredFields as $field) { $attribute = $this->getAttribute($field); if (!isset($importData[$field]) && $attribute && $attribute->getIsRequired()) { $message = Mage::helper('catalog')->__('Skip import row, required field "%s" for new products not defined', $field); Mage::throwException($message); } } if (isset($importData['category_ids'])) { $product->setCategoryIds($importData['category_ids']); } foreach ($this->_ignoreFields as $field) { if (isset($importData[$field])) { unset($importData[$field]); } } if ($store->getId() != 0) { $websiteIds = $product->getWebsiteIds(); if (!is_array($websiteIds)) { $websiteIds = array(); } if (!in_array($store->getWebsiteId(), $websiteIds)) { $websiteIds[] = $store->getWebsiteId(); } $product->setWebsiteIds($websiteIds); } if (isset($importData['websites'])) { $websiteIds = $product->getWebsiteIds(); if (!is_array($websiteIds)) { $websiteIds = array(); } $websiteCodes = split(',', $importData['websites']); foreach ($websiteCodes as $websiteCode) { try { $website = Mage::app()->getWebsite(trim($websiteCode)); if (!in_array($website->getId(), $websiteIds)) { $websiteIds[] = $website->getId(); } } catch (Exception $e) {} } $product->setWebsiteIds($websiteIds); unset($websiteIds); } foreach ($importData as $field => $value) { if (in_array($field, $this->_inventorySimpleFields)) { continue; } if (in_array($field, $this->_imageFields)) { continue; } $attribute = $this->getAttribute($field); if (!$attribute) { continue; } $isArray = false; $setValue = $value; if ($attribute->getFrontendInput() == 'multiselect') { $value = split(self::MULTI_DELIMITER, $value); $isArray = true; $setValue = array(); } if ($value && $attribute->getBackendType() == 'decimal') { $setValue = $this->getNumber($value); } if ($attribute->usesSource()) { $options = $attribute->getSource()->getAllOptions(false); if ($isArray) { foreach ($options as $item) { if (in_array($item['label'], $value)) { $setValue[] = $item['value']; } } } else { $setValue = null; foreach ($options as $item) { if ($item['label'] == $value) { $setValue = $item['value']; } } } } $product->setData($field, $setValue); } if (!$product->getVisibility()) { $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE); } $stockData = array(); $inventoryFields = $product->getTypeId() == 'simple' ? $this->_inventorySimpleFields : $this->_inventoryOtherFields; foreach ($inventoryFields as $field) { if (isset($importData[$field])) { if (in_array($field, $this->_toNumber)) { $stockData[$field] = $this->getNumber($importData[$field]); } else { $stockData[$field] = $importData[$field]; } } } $product->setStockData($stockData); //MRD added to remove all images for product before uploading new images if(isset($importData['remove_all_images']) && $importData['remove_all_images']=="yes" ){ //check if gallery attribute exists then remove all images if it exists //Get products gallery attribute $attributes = $product->getTypeInstance()->getSetAttributes(); if (isset($attributes['media_gallery'])) { $gallery = $attributes['media_gallery']; //Get the images $galleryData = $product->getMediaGallery(); foreach($galleryData['images'] as $image){ //If image exists if ($gallery->getBackend()->getImage($product, $image['file'])) { $gallery->getBackend()->removeImage($product, $image['file']); } } } } //END Remove Images $imageData = array(); foreach ($this->_imageFields as $field) { if (!empty($importData[$field]) && $importData[$field] != 'no_selection') { if (!isset($imageData[$importData[$field]])) { $imageData[$importData[$field]] = array(); } $imageData[$importData[$field]][] = $field; } } foreach ($imageData as $file => $fields) { try { // JED - changed to not mark imported images as 'excluded' - see http://www.magentocommerce.com/boards/viewthread/6971/P15/ and http://www.magentocommerce.com/boards/viewthread/40007/ //$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields); $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $file, $fields, false, false); } catch (Exception $e) {} } //Now add each gallery image. //MRD added "gallimg" to just add an image to the gallery if (!empty($importData['gallimg'])) { //$importData['gallimg'] should be a list of images to import seperated by ";" $images = explode(";", $importData['gallimg']); foreach($images as $image){ //Don't add field for gallimg's try { //made second param null so it is not added as the main image, small or thumb. just added to the gallery $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . trim($image), NULL, false, false); } catch (Exception $e) {} } } $product->setIsMassupdate(true); $product->setExcludeUrlRewrite(true); $product->save(); return true; }