1,前臺使用input-file type按鈕提交文件到magento指定的控制器,controllers獲取.csv文件,由於magento是在zend框架上實現的,能夠使用以下代碼獲取文件的上傳信息:web
1 /** 2 * New zend File Uploader 3 */ 4 $uploadsData = new Zend_File_Transfer_Adapter_Http (); 5 $filesDataArray = $uploadsData->getFileInfo ();
2,遍歷獲取的$filesDataArray文件,這裏上傳的文件包括:保存產品信息的csv文件與對應的zip產品圖片,遍歷圖片代碼以下:數據庫
1 $currentDateTime = date ( "Ymd_His", Mage::getModel ( 'core/date' )->timestamp ( time () ) ); 2 3 foreach ( $filesDataArray as $key => $value ) { 4 /** 5 * Initilize file name 6 */ 7 $filename = $key; 8 9 /** 10 * Upload csv file 11 */ 12 13 if ($key == 'bulk-product-upload-csv-file' && isset ( $filesDataArray [$filename] ['name'] ) && (file_exists ( $filesDataArray [$filename] ['tmp_name'] ))) { 14 $csvFilePath = ''; 15 $csvFilePath = array (); 16 $uploader = new Varien_File_Uploader ( $filename ); 17 $uploader->setAllowedExtensions ( array ( 18 'csv' 19 ) ); 20 $uploader->setAllowRenameFiles ( true ); 21 $uploader->setFilesDispersion ( false ); 22 $path = Mage::getBaseDir ( 'media' ) . DS . 'marketplace' . DS . 'bulk' . DS . 'product' . DS . 'csv' . DS; 23 24 $uploader->save ( $path, 'seller_' . $sellerId . '_date_' . $currentDateTime . '.csv' ); 25 $csvFilePath = $path . $uploader->getUploadedFileName (); 26 } 27 28 /** 29 * Upload csv image 30 */ 31 if ($key == 'bulk-product-upload-image-file' && isset ( $filesDataArray [$filename] ['name'] ) && (file_exists ( $filesDataArray [$filename] ['tmp_name'] ))) { 32 $uploader = new Varien_File_Uploader ( $filename ); 33 $uploader->setAllowedExtensions ( array ( 34 'zip' 35 ) ); 36 $uploader->setAllowRenameFiles ( true ); 37 $uploader->setFilesDispersion ( false ); 38 $path = Mage::getBaseDir ( 'media' ) . DS . 'marketplace' . DS . 'bulk' . DS . 'product' . DS . 'image' . DS; 39 /** 40 * Uploader save 41 */ 42 $uploader->save ( $path, 'seller_' . $sellerId . '_date_' . $currentDateTime . '.zip' ); 43 $imageFilePath = $path . $uploader->getUploadedFileName (); 44 45 $ZipFileName = $imageFilePath; 46 $homeFolder = Mage::getBaseDir ( 'media' ) . DS . 'marketplace' . DS . 'bulk' . DS . 'product' . DS . 'image' . DS . 'seller_' . $sellerId . '_date_' . $currentDateTime; 47 /** 48 * New Varien File 49 */ 50 $file = new Varien_Io_File (); 51 /** 52 * Make Directory 53 */ 54 $file->mkdir ( $homeFolder ); 55 Mage::helper ( 'marketplace/product' )->exportZipFile ( $ZipFileName, $homeFolder ); 56 } 57 }
在此之中,對遍歷到的csv和zip文件分別保存到設置的文件夾下:數組
對於csv文件設置了存儲路徑,限制了文件上傳的格式,根據商戶ID與上傳時間設置了文的存儲名稱:session
1 $uploader->save ( $path, 'seller_' . $sellerId . '_date_' . $currentDateTime . '.csv' );
對於zip的圖片文件設置了存儲路徑,限制了文件上傳的格式,根據商戶ID與上傳時間設置了文的存儲名稱:app
$uploader->save ( $path, 'seller_' . $sellerId . '_date_' . $currentDateTime . '.zip' ); $imageFilePath = $path . $uploader->getUploadedFileName (); $ZipFileName = $imageFilePath;
解壓zip圖片,用使用當前模塊中helper=>exportZipFile ( $ZipFileName, $homeFolder )對上傳的zip圖片進行解壓,代碼以下:框架
1 /** 2 * 3 * 4 * 5 * Unzip uploaded images 6 * 7 * @param string $zipFileName 8 * @return boolean 9 */ 10 public function exportZipFile($zipFileName, $homeFolder) { 11 /** 12 * New ZIP archive 13 */ 14 $zip = new ZipArchive (); 15 if ($zip->open ( $zipFileName ) === true) { 16 /** 17 * Make all the folders 18 */ 19 for($i = 0; $i < $zip->numFiles; $i ++) { 20 $onlyFileName = $zip->getNameIndex ( $i ); 21 $fullFileName = $zip->statIndex ( $i ); 22 if ($fullFileName ['name'] [strlen ( $fullFileName ['name'] ) - 1] == "/") { 23 @mkdir ( $homeFolder . "/" . $fullFileName ['name'], 0700, true ); 24 } 25 } 26 27 /** 28 * Unzip into the folders 29 */ 30 for($i = 0; $i < $zip->numFiles; $i ++) { 31 $onlyFileName = $zip->getNameIndex ( $i ); 32 $fullFileName = $zip->statIndex ( $i ); 33 34 if (! ($fullFileName ['name'] [strlen ( $fullFileName ['name'] ) - 1] == "/") && preg_match ( '#\.(jpg|jpeg|gif|png)$#i', $onlyFileName )) { 35 copy ( 'zip://' . $zipFileName . '#' . $onlyFileName, $homeFolder . "/" . $fullFileName ['name'] ); 36 } 37 } 38 $zip->close (); 39 } else { 40 Mage::getSingleton ( 'core/session' )->addError ( $this->__ ( "Error: Can't open zip file" ) ); 41 } 42 return true; 43 }
把csv文件轉換爲數組形式,使用當前模塊中helper=>convertCsvFileToUploadArray ( $csvFilePath )進行轉換,代碼以下:ide
1 /** 2 * 3 * 4 * 5 * Convert csv file to upload array 6 * 7 * @param string $csvFilePath 8 * @return array 9 */ 10 public function convertCsvFileToUploadArray($csvFilePath) { 11 $productInfo = array (); 12 if (! empty ( $csvFilePath )) { 13 /** 14 * Initializing new varien file 15 */ 16 $csv = new Varien_File_Csv (); 17 $data = $csv->getData ( $csvFilePath ); 18 $line = $lines = ''; 19 20 $keys = array_shift ( $data ); 21 22 /** 23 * Getting instance for catalog product collection 24 */ 25 $productInfo = $createProductData = array (); 26 foreach ( $data as $lines => $line ) { 27 if (count ( $keys ) == count ( $line )) { 28 $data [$lines] = array_combine ( $keys, $line ); 29 } 30 } 31 32 if (count ( $data ) <= 1 && count ( $keys ) >= 1 && ! empty ( $line ) && count ( $keys ) == count ( $line )) { 33 $data [$lines + 1] = array_combine ( $keys, $line ); 34 } 35 36 $createProductData = $this->uploadProductData ( $data ); 37 38 if (! empty ( $createProductData )) { 39 $productInfo [] = $createProductData; 40 } 41 } 42 43 return $productInfo; 44 }
只要是獲取csv表格頭部的命名(數據庫字段)使用 $data [$lines] = array_combine ( $keys, $line ),轉換爲如下標爲字段名,對應爲值得二維數組ui
3,使用一下方法處理收集到的數據,代碼以下:this
1 2 /** 3 * 4 * @param string $imageFilePath 5 * @param array $productData 6 * @param string $homeFolder 7 * @param string $csvFilePath 8 * @return boolean 9 */ 10 public function bulkproductuploadfuncationality($imageFilePath, $productData, $homeFolder, $csvFilePath) { 11 if (file_exists ( $imageFilePath )) { 12 /** 13 * Delete images from temporary zip folder 14 */ 15 unlink ( $imageFilePath ); 16 } 17 18 if (isset ( $productData [0] )) { 19 $configurableAttributes = array (); 20 /** 21 * Get Configurable Products 22 */ 23 $configurableAttributes = $this->getRequest ()->getPost ( 'configurable_attribute' ); 24 Mage::helper ( 'marketplace/image' )->saveProductData ( $productData [0], $homeFolder, $configurableAttributes ); 25 if (Mage::getStoreConfig ( 'marketplace/product/save_uploadfiles' ) != 1) { 26 if (file_exists ( $csvFilePath )) { 27 /** 28 * Delete csv file 29 */ 30 unlink ( $csvFilePath ); 31 } 32 33 /** 34 * Delete images from temporary zip folder 35 */ 36 Mage::helper ( 'marketplace/image' )->rrmdir ( $homeFolder ); 37 } 38 $this->_redirect ( 'marketplace/product/manage/' ); 39 } else { 40 /** 41 * Add Notice 42 */ 43 Mage::getSingleton ( 'core/session' )->addNotice ( Mage::helper ( 'marketplace' )->__ ( 'No data found' ) ); 44 $this->_redirect ( 'marketplace/product/manage/' ); 45 return true; 46 } 47 }
對於zip的圖片文件進行刪除,並使用saveProductData($productData, $imagePath, $configurableAttributes) 對存儲數據的結果進行消息返回提示,在此之中會提示重複的sku,gtin碼的驗證結果及是否重複,或者上傳成功產品數量,此方法中使用saveBulkUploadProduct ( $productData, $imagePath,$configurableAttributes, $rowcountForImport )對產品信息存入數據庫,代碼以下:spa
1 /** 2 * Save bulk upload product 3 * 4 * @param array $productData 5 * @param array $imagePath 6 * @param array $configurableAttributes 7 * @param number $rowcountForImport 8 * @return array $productCountArray 9 */ 10 public function saveBulkUploadProduct($productData, $imagePath, $configurableAttributes, $rowcountForImport) { 11 $countries = Mage::getModel ( 'marketplace/bulk' )->getContriesValue (); 12 /** 13 * Initilize website ids 14 */ 15 $websiteIds = array ( 16 Mage::app ()->getStore ( true )->getWebsite ()->getId () 17 ); 18 $importProductsCount = 0; 19 $existSkuCounts = 0; 20 $existGtinCounts = 0; 21 $notValidGtinsCounts = 0; 22 $existGtinLists = array(); 23 $notValidGtinsArr=array(); 24 $productCountArray = array (); 25 foreach ( $productData ['sku'] as $key => $value ) { 26 $flag = Mage::getModel ( 'marketplace/bulk' )->checkRequiredFieldForBulkUpload ( $productData, $key ); 27 if ($flag == 1) { 28 $images = array (); 29 $checkSkuAndGtinModel= Mage::getModel ( 'catalog/product' ); 30 $productSkuForCheck =$checkSkuAndGtinModel->getIdBySku ( $productData ['sku'] [$key] ); 31 if ($productSkuForCheck) { 32 $existSkuCounts = $existSkuCounts + 1; 33 continue; 34 } 35 $gtinCode= $productData ['gtin'] [$key]; 36 $collection =$checkSkuAndGtinModel->getCollection ()->addAttributeToFilter ( 'gtin', $gtinCode ); 37 $count = count ( $collection ); 38 if($count){ 39 $existGtinLists[]=$gtinCode; 40 $existGtinCounts=$existGtinCounts+1; 41 continue; 42 } 43 if(!preg_match('/^[0-9]{12,13}$ /', $gtinCode)){ 44 $notValidGtinsArr[]=$gtinCode; 45 $notValidGtinsCounts=$notValidGtinsCounts+1; 46 continue; 47 } 48 $orFlag = Mage::getModel ( 'marketplace/bulk' )->checkProductTypeForBulkUpload ( $productData, $key ); 49 if ($orFlag == 1) { 50 $product = Mage::getModel ( 'catalog/product' ); 51 $categoryIds = array (); 52 /** 53 * Multi row product data 54 */ 55 $attributeSetName = $productData ['_attribute_set'] [$key]; 56 $sku = $productData ['sku'] [$key]; 57 $name = $productData ['name'] [$key]; 58 $gtin = $productData ['gtin'] [$key]; 59 $description = $productData ['description'] [$key]; 60 $shortDescription = $productData ['short_description'] [$key]; 61 $price = $productData ['price'] [$key]; 62 $type = $productData ['_type'] [$key]; 63 $weight = Mage::getModel ( 'marketplace/bulk' )->getWeightForBulkUpload ( $productData, $key, $type ); 64 /** 65 * Getting special price values 66 */ 67 $specialPrice = $productData ['special_price'] [$key]; 68 $specialDate = array (); 69 $specialDate ['special_from_date'] = $productData ['special_from_date'] [$key]; 70 $specialDate ['special_to_date'] = $productData ['special_to_date'] [$key]; 71 /** 72 * Fetch images info for product 73 */ 74 $images = Mage::getModel ( 'marketplace/bulk' )->getImagesForBulkProduct ( $key, $productData ); 75 $categoryIds = Mage::getModel ( 'marketplace/bulk' )->getCategoryIdsForBulk ( $key, $productData ); 76 $customOptions = Mage::getModel ( 'marketplace/bulk' )->getCustomOptionsForBulk ( $key, $productData, $rowcountForImport ); 77 $dataForBulkUpload = Mage::getModel ( 'marketplace/bulk' )->getDataForBulkProduct ( $key, $productData ); 78 /** 79 * Fetch attribute set id by attribute set name 80 */ 81 $entityTypeId = Mage::getModel ( 'eav/entity' )->setType ( 'catalog_product' )->getTypeId (); 82 $attributeSetId = Mage::getModel ( 'eav/entity_attribute_set' )->getCollection ()->setEntityTypeFilter ( $entityTypeId )->addFieldToFilter ( 'attribute_set_name', $attributeSetName )->getFirstItem ()->getAttributeSetId (); 83 84 if (empty ( $attributeSetId )) { 85 $attributeSetId = Mage::getModel ( 'eav/entity_attribute_set' )->getCollection ()->setEntityTypeFilter ( $entityTypeId )->addFieldToFilter ( 'attribute_set_name', 'Default' )->getFirstItem ()->getAttributeSetId (); 86 } 87 88 $product->setSku ( $sku ); 89 $product->setName ( $name ); 90 $product->setGtin ( $gtin ); 91 $product->setDescription ( $description ); 92 $product->setShortDescription ( $shortDescription ); 93 $product->setPrice ( $price ); 94 /** 95 * Set product data for bulk product upload 96 */ 97 $product = Mage::getModel ( 'marketplace/bulk' )->setProductDataForBulkProductUpload ( $product, $specialPrice, $specialDate, $type, $weight, $attributeSetId, $categoryIds ); 98 $product = Mage::getModel ( 'marketplace/bulk' )->setProductInfoForBulkProductUpload ( $dataForBulkUpload, $productData, $key, $product, $websiteIds, $countries ); 99 $product = Mage::getModel ( 'marketplace/bulk' )->setImagesAndCustomOptionForBulkProductUpload ( $product, $images, $imagePath, $customOptions ); 100 /** 101 * Fetch configurable product options 102 */ 103 $configurableProductsDataBulk = Mage::getModel ( 'marketplace/bulk' )->getConfigurableProductDataForBulkUpload ( $key, $type, $productData ); 104 $attributeIds = $configurableProductsDataBulk ['attribute_ids']; 105 $configurableProductsData = $configurableProductsDataBulk ['configurable_products_data']; 106 $superProductsSkus = $configurableProductsDataBulk ['super_products_skus']; 107 $product = Mage::getModel ( 'marketplace/bulk' )->setConfigurableProductDataForBulkUpload ( $product, $attributeIds, $type, $attributeSetId, $configurableProductsData ); 108 $product = Mage::getModel ( 'marketplace/bulk' )->setProductConfigData ( $productData, $product, $configurableAttributes, $key ); 109 110 /** 111 * Initialize configurable product options 112 */ 113 $product->save (); 114 Mage::getModel ( 'marketplace/bulk' )->saveSimpleProductForBulkUpload ( $type, $superProductsSkus, $product ); 115 116 Mage::getSingleton ( 'catalog/product_option' )->unsetOptions (); 117 $importProductsCount = $importProductsCount + 1; 118 } 119 } 120 } 121 /** 122 * Initilize rpoduct count array 123 */ 124 $productCountArray ['import_products_count'] = $importProductsCount; 125 $productCountArray ['exist_sku_counts'] = $existSkuCounts; 126 $productCountArray ['exist_gtin_counts'] = $existGtinCounts; 127 $productCountArray ['exist_gtin_lists'] = $existGtinLists; 128 $productCountArray ['not_valid_gtin_counts'] = $notValidGtinsCounts; 129 $productCountArray ['not_valid_gtin_lists'] = $notValidGtinsArr; 130 return $productCountArray; 131 }
在此之中,會用到magento特有的model方法,把數據存入數據庫,最經常使用的collection,resource等
總結:整個過程下來,一個模塊中的model,helper方法混合調用,使用magento特有的model方法操做數據比較方便,使用zend框架處理文件上傳與csv文件轉數據也比較省事