public function testUpdateImage() { $product = new Mage_Catalog_Model_Product(); $product->setData('media_gallery', array('images' => array('image' => array('file' => 'magento_image.jpg')))); $this->_model->updateImage($product, 'magento_image.jpg', array('label' => 'test label')); $this->assertEquals('test label', $product->getData('media_gallery/images/image/label')); }
/** * Save product (import) * * @param array $importData * @throws Mage_Core_Exception * @return bool */ public function saveRow(array $importData) { $product = $this->getProductModel()->reset(); if (empty($importData['store'])) { if (!is_null($this->getBatchParams('store'))) { $store = $this->getStoreById($this->getBatchParams('store')); } else { $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', 'store'); Mage::throwException($message); } } else { $store = $this->getStoreByCode($importData['store']); } if ($store === false) { $message = Mage::helper('catalog')->__('Skipping import row, store "%s" field does not exist.', $importData['store']); Mage::throwException($message); } if (empty($importData['sku'])) { $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', 'sku'); Mage::throwException($message); } $product->setStoreId($store->getId()); $productId = $product->getIdBySku($importData['sku']); if ($productId) { $product->load($productId); } else { $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, the value "%s" is invalid 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')->__('Skipping import row, required field "%s" for new products is not defined.', $field); Mage::throwException($message); } } } // process row with media data only if (isset($importData['_media_image']) && strlen($importData['_media_image'])) { $this->saveImageDataRow($product, $importData); return true; } $this->setProductTypeInstance($product); 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) || !$store->getId()) { $websiteIds = array(); } $websiteCodes = explode(',', $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->_inventoryFields)) { continue; } if (is_null($value)) { continue; } $attribute = $this->getAttribute($field); if (!$attribute) { continue; } $isArray = false; $setValue = $value; if ($attribute->getFrontendInput() == 'multiselect') { $value = explode(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 = false; foreach ($options as $item) { if (is_array($item['value'])) { foreach ($item['value'] as $subValue) { if (isset($subValue['value']) && $subValue['value'] == $value) { $setValue = $value; } } } else { 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 = isset($this->_inventoryFieldsProductTypes[$product->getTypeId()]) ? $this->_inventoryFieldsProductTypes[$product->getTypeId()] : array(); 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); $arrayToMassAdd = array(); foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) { if (isset($importData[$mediaAttributeCode])) { $file = trim($importData[$mediaAttributeCode]); if (!empty($file) && !$this->_galleryBackendModel->getImage($product, $file)) { $arrayToMassAdd[] = array('file' => trim($file), 'mediaAttribute' => $mediaAttributeCode); } } } $addedFilesCorrespondence = $this->_galleryBackendModel->addImagesWithDifferentMediaAttributes($product, $arrayToMassAdd, Mage::getBaseDir('media') . DS . 'import', false, false); foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) { $addedFile = ''; if (isset($importData[$mediaAttributeCode . '_label'])) { $fileLabel = trim($importData[$mediaAttributeCode . '_label']); if (isset($importData[$mediaAttributeCode])) { $keyInAddedFile = array_search($importData[$mediaAttributeCode], $addedFilesCorrespondence['alreadyAddedFiles']); if ($keyInAddedFile !== false) { $addedFile = $addedFilesCorrespondence['alreadyAddedFilesNames'][$keyInAddedFile]; } } if (!$addedFile) { $addedFile = $product->getData($mediaAttributeCode); } if ($fileLabel && $addedFile) { $this->_galleryBackendModel->updateImage($product, $addedFile, array('label' => $fileLabel)); } } } $product->setIsMassupdate(true); $product->setExcludeUrlRewrite(true); $product->save(); // Store affected products ids $this->_addAffectedEntityIds($product->getId()); return true; }