记录magento通过csv文件与zip(图片压缩)上传产品到数据库的过程
1,前台使用input-file type按钮提交文件到magento指定的控制器,controllers获取.csv文件,因为magento是在zend框架上实现的,可以使用如下代码获取文件的上传信息:
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与上传时间设置了文的存储名称:
1 $uploader->save ( $path, 'seller_' . $sellerId . '_date_' . $currentDateTime . '.csv' );
对于zip的图片文件设置了存储路径,限制了文件上传的格式,根据商户ID与上传时间设置了文的存储名称:
$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 )进行转换,代码如下:
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 ),转换为以下标为字段名,对应为值得二维数组
3,使用一下方法处理收集到的数据,代码如下:
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 )对产品信息存入数据库,代码如下:
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文件转数据也比较省事