Magento产品多图片批量上传

2,722次阅读
没有评论

共计 6906 个字符,预计需要花费 18 分钟才能阅读完成。

Magento添加产品是件很头疼的事情,因此我们就就需要批量上传。magento默认不支持多图片批量上传,要经过一些修改才可以,我转载了magentochina蟋蟀的文章过来,嘿嘿……

一、到/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
/**
 * Import Multiple Images during Product Import
 *
 */

class YDL_ImportMultipleImages_Model_Convert_Adapter_Product extends Mage_Catalog_Model_Convert_Adapter_Product
{
 /**
 * 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')->__('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();

 /**
 * 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, is not valid value "%s" 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')->__('Skip import row, required field "%s" for new products not defined', $field);
 Mage::throwException($message);
 }
 }
 }

 $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)) {
 $websiteIds = array();
 }
 $websiteCodes = split(',', $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 (in_array($field, $this->_imageFields)) {
 continue;
 }

 $attribute = $this->getAttribute($field);
 if (!$attribute) {
 continue;
 }

 $isArray = false;
 $setValue = $value;

 if ($attribute->getFrontendInput() == 'multiselect') {
 $value = split(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 = null;
 foreach ($options as $item) {
 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);

 $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) {}
 }

 /**
 * Allows you to import multiple images for each product.
 * Simply add a 'gallery' column to the import file, and separate
 * each image with a semi-colon.
 */
 try {
 $galleryData = explode(';',$importData["gallery"]);
 foreach($galleryData as $gallery_img)
 /**
 * @param directory where import image resides
 * @param leave 'null' so that it isn't imported as thumbnail, base, or small
 * @param false = the image is copied, not moved from the import directory to it's new location
 * @param false = not excluded from the front end gallery
 */
 {
 $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $gallery_img, null, false, false);
 }
 }
 catch (Exception $e) {}        
 /* End Modification */

 $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网店中。
Magento批量上传图片已经成功了。

正文完
 0
评论(没有评论)

空瓶子部落

文章搜索
推荐阅读
钢中的马氏体、贝氏体与珠光体组织傻傻分不清?

钢中的马氏体、贝氏体与珠光体组织傻傻分不清?

在金属材料科学中,钢的组织结构对其性能具有决定性的影响。钢的组织主要包括马氏体、贝氏体和珠光体等,这些组织的形...
深藏不露的九宫格,用一用就知道你的逻辑有多强!

深藏不露的九宫格,用一用就知道你的逻辑有多强!

你有没有听说过九宫格?据说最早是中国古代用来进行数学计算的工具。不过,现在的九宫格可不只限于数学,它已经变成了...
什么是质量检验?质量检验的方式有哪些?

什么是质量检验?质量检验的方式有哪些?

质量检验是指对产品的一种或多种特性进行测量、检查、试验、计量,并将这些特性与规定的要求进行比较,以确定其符合性...
如何跳出盒子,打破框架?这三个思考工具挺有用!

如何跳出盒子,打破框架?这三个思考工具挺有用!

做过分析和咨询的伙伴们都知道框架,有人说框架千篇一律,阻碍了创新;有人说框架让他们摆脱了漫无目的的摸索,找到了...
【支招】SQE一定会用到的24个图解工具

【支招】SQE一定会用到的24个图解工具

导 语introduction 模型和工具的有效应用能很大程度上帮助采购人员对采购过程进行系统化的梳理,并提高...
最新文章
群晖 Let’s Encrypt 泛域名证书自动更新

群晖 Let’s Encrypt 泛域名证书自动更新

目前acme协议版本更新,开始支持泛域名(wildcard),也就是说,可以申请一个类似*.domain.co...
可以卸载TV Box 了,这款支持「绅士模式」的影视神器你值得拥有

可以卸载TV Box 了,这款支持「绅士模式」的影视神器你值得拥有

还在为找优秀片源难、广告多、平台会员太贵而烦恼?今天给大家挖到一款真正的影视宝藏工具——小猫影视! 作为开源免...
【收藏】一次性解决TV点播/直播自由

【收藏】一次性解决TV点播/直播自由

很多时候,资源就在面前,但是我们视而不见,因为长久的安逸,已经让人失去动手的兴趣。但是每次我需要挨个切换APP...
OpenWrt 存储空间扩容的两种方案

OpenWrt 存储空间扩容的两种方案

说明:当我们通过群晖 VMM 虚拟机安装 Open­Wrt 时,默认会分配一个 10GB 的存储空间,而实际情...
OpenWrt修改IP地址两种方法(直接命令修改跟后台修改)

OpenWrt修改IP地址两种方法(直接命令修改跟后台修改)

OpenWrt是什么?OpenWrt一般常见于无线路由器(软路由)第三方固件,它是一个高效、可靠、功能多的路由...
热门文章
提高过程能力指数(CP/CPK)的途径

提高过程能力指数(CP/CPK)的途径

编者按:过程能力指数(CP/CPK)想必各位质量人都耳熟能详、运用自如,质量工程师之家前期也共享过数篇关于过程...
SPC控制图的八种模式分析

SPC控制图的八种模式分析

SPC控制图有八种模式,即八种判断异常的检验准则,每一种检验准则代表一种异常现象,应用SPC控制图进行过程评估...
测量高手放大招:圆跳动测量技巧总结

测量高手放大招:圆跳动测量技巧总结

01. 前言 在五金机加工厂实际的测量工作中,经常碰到要求测量两个要素的圆跳动问题, 利用不同的测量辅件及夹具...
过程能力分析(CP&cpk)

过程能力分析(CP&cpk)

引入过程能力分析的目的? 在我们现有的管理过程中,我们经常会遇到有些具体指标总是不尽人意,存在许多需要改进的地...
新能源汽车 “两会”精神宣贯会

新能源汽车 “两会”精神宣贯会

此次和大家分享新能源汽车相关政策: [embeddoc url=”https://www.ctro...
最新评论
多乐士 多乐士 通过摸索发现ssh拉取会报错,直接网页访问下载会报404错误,不知道原因;但是可以通过群晖CM注册表访问下载,其方法如下: Container Manager-注册表-设置-新增-注册表名称随便写,注册表URL填你的加速地址,勾选信任的SSL自我签署证书,登录信息不填-应用-使用你的地址,这是注册表会显示了,在搜索栏中输入映像名称,搜索结果在每一页的最后一个,你需要划到最后一个进行下载,实测可正常下载安装。 以上供网友参考。
多乐士 多乐士 还有一个比较简单的方法,只是需要一些外部工具。 1、讲损毁硬盘取出,装入外部移动硬盘 2、打开Diskgenius,定位到硬盘 3、格式化系统分区 4、重新插入硬盘 5、存储池->修复存储池即可
多乐士 多乐士 写的不错的文章
辞了老衲 辞了老衲 这个确实有帮助。
渋驀 渋驀 当然任何时候都可以用curl命令和crontab来实现动态更新DDNS的ip地址: 1、安装crontab之后为root用户创建文件/var/spool/cron/root 2、创建并配置ddnsupdate.sh,放到/usr/bin/文件下,文件内容(以he.net为例): Autodetect my IPv4/IPv6 address: IPV4:curl -4 "http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com" IPV6:curl -6 "http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com" 3、添加执行权限chomod +x /usr/bin/ddnsupdate.sh 4、编辑root用户的crontab:*/10 * * * * /usr/binddnsupdate.sh,每10分钟执行一次。好了,可以享受你的DDNS了
21410 21410 请问下载链接在那里?
madkylin madkylin 不错,不错,谢谢分享了,好东西啊 :lol:
feilung feilung 求方法
zengsuyi zengsuyi 应该挺不错的
zise zise 看看是怎么操作的。。 :oops: