如何解决Magento批量上传产品的时候图片被Exclude问题
2013-06-15 10:44
465 查看
如何解决Magento批量上传产品的时候图片被Exclude问题
10-12-14发布者: Ansy Huang
很多用户在使用批量导入magento产品时会遇到这种情况,图片不在前台显示了。碰到这样的情况的时候会让人着急.结果去magneto后台管理界面->Catalog->Manage Products(管理商品),任意点击一个已经上传的成功的商品,你会看到产品的Images选项自动选择了excluded(排除)所有的图片。
解决方法:我们通过找到这个路径找到相关的文件:app/code/core/Mage/Catalog/Model/Convert/Adapter找到Product.php文件中697行:
$product->addImageToMediaGallery(Mage::getBaseDir(’media’) .DS .‘import’ .$file, $fields); 修改为$product->addImageToMediaGallery(Mage::getBaseDir(’media’) .DS .‘import’ .$file, $fields,false,false);
本文在Magento CE 1.7.0.1版本下通过。
Magento通过csv表格批量导入产品时,不可避免的要遇到多个产品图片导入的问题。 不知道为什么,一直在细节上把握很好的Magento,竟没有解决这个重要问题。
解决方法:复制文件下面文件到local代码池
view source
print?
1 | app/code/core/Mage/Catalog/Model/Convert/Adapter/Product.php |
view source
print?
01 | public function saveRow( array $importData ) |
02 | { |
03 | // ... |
04 |
05 | // 导入多图逻辑开始 |
06 | try { |
07 | $galleryData = explode ( ';' , $importData [ "gallery" ]); |
08 |
09 | foreach ( $galleryData as $gallery_img ) |
10 | { |
11 | /** |
12 | * @param directory where import image resides |
13 | * @param leave 'null' so that it isn't imported as thumbnail, base, or small |
14 | * @param false =the image is copied, not moved from the import directory to it's new location |
15 | * @param false =not excluded from the front end gallery |
16 | */ |
17 | $product ->addImageToMediaGallery(Mage::getBaseDir( 'media' ) .DS . 'import' . $gallery_img , null, false, false); |
18 | } |
19 | } |
20 | catch (Exception $e ) {} |
21 | // 导入多图逻辑结束 |
22 |
23 | $product ->setIsMassupdate(true); |
24 | $product ->setExcludeUrlRewrite(true); |
25 | $product ->save(); |
26 | } |
假设某产品有三张图片,位于media/import/2012/文件夹下,分别是a.jpg, b.jpg, c.jpg,其中a.jpg为首图。则导入csv表格(片段)如下:
view source
print?
1 | image small_image thumbnailgallery |
2 | ----------- ----------- ----------- ----------------------- |
3 | /2012/a.jpg /2012/a.jpg /2012/a.jpg /2012/b.jpg;/2012/c.jpg |
往magento里面批量上传多个产品是件比较麻烦的事情,但工作效率却非常高,所以大家都愿意使用这种方法上传产品,特别是在产品很多的情况下。在这里我给大家详细解说一下如何批量上传单产品多图片货品的方法。相信大家按照我写的步骤来,最后是会迈向成功的,获取成就的喜悦!如果有任何疑问,大家可以给我留言。
下面是详细步骤第一步:
到/app/etc/modules/
目录下创建文件并命名为YDL_ImportMultipleImages.xml
该文件会告诉Magento你有这样的模块,以及它的位置。代码如下:
<?xml version="1.0"?> <config> <modules> <YDL_ImportMultipleImages> <active>true</active> <codePool>local</codePool> </YDL_ImportMultipleImages> </modules> </config>
第二步: 创建文件 /app/code/local/YDL/ImportMultipleImages/etc/config.xml
这个文件是你的模块配置文 件。它告诉 Magento哪个阶级我们将重写。详细点就是到你的/app/code/local
先新建YDL文件夹,再进入YDL里面新建ImportMultipleImages 文件夹,接着再进入里面新建etc文件夹,最后进入新建config.xml 文件.
代码如下:
<?xml version="1.0"?> <config> <modules> <YDL_ImportMultipleImages> <version>0.1.0</version> </YDL_ImportMultipleImages> </modules> <global> <models> <catalog> <rewrite> <!-- Override Mage_Catalog_Model_Convert_Adapter_Product --> <convert_adapter_product>YDL_ImportMultipleImages_Model_Convert_Adapter_Product</convert_adapter_product> </rewrite> </catalog> </models> </global> </config>
第三步:
创建文件/app/code/local/YDL/ImportMultipleImages/Model/Convert/Adapter/Product.php
此文件中扩大了saveRow() 类方法,这样保证了当你的magento升级后仍然能够使用多图片批量上传功能。代码如下:
<?php class YDL_ImportMultipleImages_Model_Convert_Adapter_Product extends Mage_Catalog_Model_Convert_Adapter_Product { 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')->__('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); } else { $productTypes =$this->getProductTypes(); $productAttributeSets =$this->getProductAttributeSets(); 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);
pes[$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); $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 { $product->addImageToMediaGallery(Mage::getBaseDir('media') .DS .'import' .$file, $fields); } catch (Exception $e) {} } try { $galleryData =explode(';',$importData["gallery"]); foreach($galleryData as $gallery_img) { $product->addImageToMediaGallery(Mage::getBaseDir('media') .DS .'import' .$gallery_img, null, false, false); } } catch (Exception $e) {}$product->setIsMassupdate(true); $product->setExcludeUrlRewrite(true); $product->save(); return true; } }
第四步:
现在可以开始进行批量上传了!哈哈,不过在上传之前还有很重要的事要做,不然会前功尽弃,就是在编写好你的csv文件后,需要在你的csv文件里增加一列并命名为gallery,然后在此列中把你想要上传的产品图片分别用半角英文分号“;” 隔开,举个例子吧:
你的gallery 这一列 必需类似于
/ image1.jpg;/ image2.jpg;/ image3.jpg
好了,基本上大功告成了,你可以在后台->系统(System)->设置(Configuration)->高级(Advanced)里面高级选项-“模块输出”里看到你添加的模块YDL_ImportMultipleImages。
只要你csv文件里的其它产 品属性字段没有错误,保证你的多个图片能成功的显示在你的magento网店中。最终在前台显示的结果如下图:
到此,成功实现单产品多图批量上传效果。
相关文章推荐
- 无需翻墙,wish大卖家告诉你最好用的wish商户平台产品上传免费视频教程,轻松解决图片上传不了的问题。
- linux系统 discuz论坛 门户发布新闻的时候上传图片出现上述问题“上传失败-103”的解决办法
- 如何解决博客中暂时无法上传图片或者图片上传的大小不能超过*M的问题,可无限制插入图片
- android一个上传图片的例子,包括如何终止上传过程,如果在上传的时候更新进度条(二)
- html的img中使用SVG图片做SRC在本地测试可用但是上传服务器后不能显示问题解决 标签: SVGMIME TYPE不显示服务器设置 2016-05-21 13:49 2517人阅读 评论(0)
- 解决imageView加载图片时候造成内存溢出的问题
- Android开发中如何解决加载大图片时内存溢出的问题
- jQuery插件之ajaxFileUpload(异步上传图片并实时显示,并解决onchange后ajaxFileUpload失效问题)
- 网站后台不能添加图片,如果是服务器权限问题,应该如何解决?方法如下
- IOS应用开发-图片处理(拉伸图片 创建缩略图 解决图片旋转的问题 图片编码及上传 将图片写入磁盘)
- IE7在图片上传时,无法预览本地图片的问题解决方法
- 解决WINSERVER 2008 ASP页面中上传图片大小限制问题
- 解决关于Wordpress在SAE平台上无法上传图片的问题
- android学习笔记----解决android有的手机拍照后上传图片被旋转的问题
- 解决android有的手机拍照后上传图片被旋转的问题
- js,angularjs 图片上传服务器,并解决照片旋转问题,前端图片压缩上传
- Android开发中如何解决加载大图片时内存溢出的问题
- (转) 淘淘商城系列——解决KindEditor上传图片浏览器兼容性问题
- 今天再做上传图片时候遇到了一个JS 图片预览问题
- 解决 jeesite 项目部署到linux 服务器上 ckfinder 无法上传图片的问题