您的位置:首页 > 其它

如何解决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
在saveRow()函数的结尾部分增加导入多图Gallery的逻辑。如下:

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
}
导入多图时,相应地在csv表格中增加一列,列名为:gallery。多张图片之间以英文分号(;)来分开,并去掉首图。

假设某产品有三张图片,位于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
需要注意的是:当产品文字信息变化了,在csv表格中修改,然后重复导入,这样会造成产品图片重复,可以先删除掉该产品再导入。另外一个好方法是: 删掉csv表格中的image, small_image, thumbnail, gallery等列,再导入。


往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网店中。最终在前台显示的结果如下图:

到此,成功实现单产品多图批量上传效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐