Magento 对csv表格的导入功能 高级自定义部分!,magentocsv


magento自己带有导入导出功能

后台system-->import/export-->Advanced Profiles

进入后点击add new profiles

就可以新建一个规则了

Profile Name *是名字

Actions XML *是对应的参数

譬如例子:

<action type="dataflow/convert_adapter_io" method="load">
    <var name="type">file</var>
    <var name="path">var/import</var>
    <var name="filename"><![CDATA[configproduct.csv]]></var>
    <var name="format"><![CDATA[csv]]></var>
</action>

<action type="dataflow/convert_parser_csv" method="parse">
    <var name="delimiter"><![CDATA[,]]></var>
    <var name="enclose"><![CDATA["]]></var>
    <var name="fieldnames">true</var>
    <var name="store"><![CDATA[0]]></var>
    <var name="number_of_records">1</var>
    <var name="root_catalog_id"><![CDATA[2]]></var>
    <var name="reimport_images"><![CDATA[true]]></var>
    <var name="deleteall_andreimport_images"><![CDATA[true]]></var>
    <var name="exclude_images"><![CDATA[false]]></var>
    <var name="exclude_gallery_images"><![CDATA[false]]></var>
    <var name="decimal_separator"><![CDATA[.]]></var>
    <var name="adapter">catalog/convert_adapter_productimport</var>
    <var name="method">parse</var>
</action>

解析:

 

file是文件类型

path是csv文件路径

filename是csv文件名字

format是csv格式

后面的配置是相应的文件格式

用他,可以导入您想要导入的数据,也就是magento实现了这个csv导入的框架,在相应的文件

catalog/convert_adapter_productimport

public function saveRow( array $importData )函数里面$importData

这个数据就是csv文件中的一行,每一列是这个数组中的一个元素!

然后使用magento的机制保存就可以了

上面我只是大致的一说,具体的研究,您可以下载一个免费的magento导入导出插件。然后研究里面的代码,然后就是研究如何插入数据,譬如写一个给现有产品批量导入tag的函数,例子如下:

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Catalog
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */


class Mage_Catalog_Model_Convert_Adapter_Productimport
    extends Mage_Eav_Model_Convert_Adapter_Entity
{
   
   
   public function parse()
    {
        $batchModel = Mage::getSingleton('dataflow/batch');
        /* @var $batchModel Mage_Dataflow_Model_Batch */

        $batchImportModel = $batchModel->getBatchImportModel();
        $importIds = $batchImportModel->getIdCollection();

        foreach ($importIds as $importId) {
            //print '<pre>'.memory_get_usage().'</pre>';
            $batchImportModel->load($importId);
            $importData = $batchImportModel->getBatchData();

            $this->saveRow($importData);
        }
    }
   
   
   public function saveRow( array $importData )
    {
		$default_type = 1; //amazon
		$default_status = 1; //weicaiji
		
		if(!isset($importData['type'])){
			$type = $default_type;
		}else{
			$type = $importData['type'];
		}
		$create_date_time = Mage::getModel("core/date")->date("Y-m-d H:i:s");
        if ( isset( $importData['sku'] ) && $importData['sku'] !="" ) {
			$sku = $importData['sku'];
			$no = $importData['no'];
			$pro_sku = Mage::getModel("getreview/sku");
			$pro = $pro_sku	->getCollection()
							->addFieldToFilter("sku",array("eq"=>$type))
							->addFieldToFilter("no",array("eq"=>$no))
							->addFieldToFilter("type",array("eq"=>$type))
							->getFirstItem()
						;
			if($pro->getId()){
				$this->addException(
                       "this sku:".$sku.",no:".$no.",type:".$type." is exist;"
                    );
				return ture;
			}
            try {
                $pro_sku->setType($type)
						->setStatus($default_status)
						->setCreateDateTime($create_date_time)
						->setSku($sku)
						->setNo($no)
						->save()
						;
            
            }catch ( Exception $e ) {
              
                    
            }

           
        }


       
        return true;
    }
	
}


相关内容

    暂无相关文章