magento单产品多图片批量上传方法

本文详细介绍如何在Magento中批量上传带有多个图片的产品,通过创建自定义模块实现多图片导入功能,确保产品信息准确无误地展示在电商网站上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

往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);
 }
 $product->setTypeId($productTypes[strtolower($importData['type'])]);
 
 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) {}
 }

 
 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网店中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值