网络技术
当前位置 :首页信息技术网络技术

magento export & import categories 导入目录

来源: 作者: 时间:2010-10-29 点击:

1.创建文件:app/code/local/Mycomp/Catalog/Model/Convert/Adapter/Category.php:

<?php

class 
Mycomp_Catalog_Model_Convert_Adapter_Category
    
extends 
Mage_Eav_Model_Convert_Adapter_Entity
{
    
protected $_categoryCache 
= array();

    protected 
$_stores
;

    
/**
     * Category display modes
     */
    
protected $_displayModes = array( 'PRODUCTS''PAGE''PRODUCTS_AND_PAGE'
);

    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
);
        
}
    }

    
/**
     * Save category (import)
     *
     * @param array $importData
     * @throws Mage_Core_Exception
     * @return bool
     */
    
public function saveRow(array $importData
)
    
{
        
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
);
        
}

        $rootId 
$store->getRootCategoryId
();
        if (!
$rootId
{
            
return array();
        
}
        $rootPath 
'1/'.$rootId
;
        if (empty(
$this->_categoryCache[$store->getId()])) 
{
            $collection 
Mage::getModel('catalog/category')->getCollection
()
                ->
setStore($store
)
                ->
addAttributeToSelect('name'
);
            
$collection->getSelect()->where("path like '".$rootPath."/%'"
);

            foreach (
$collection as $cat
{
                $pathArr 
explode('/'$cat->getPath
());
                
$namePath ''
;
                for (
$i=2$l=sizeof($pathArr); $i<$l$i++) 
{
                    $name 
$collection->getItemById($pathArr[$i])->getName
();
                    
$namePath .= (empty($namePath) ? '' '/').trim($name
);
                
}
                $cat
->setNamePath($namePath
);
            
}

            $cache 
= array();
            foreach (
$collection as $cat
{
                $cache[strtolower
($cat->getNamePath())$cat
;
                
$cat->unsNamePath
();
            
}
            $this
->_categoryCache[$store->getId()$cache
;
        
}
        $cache 
=& $this->_categoryCache[$store->getId()]
;

        
$importData['categories'preg_replace('#s*/s*#''/'trim($importData['categories']
));
        if (!empty(
$cache[$importData['categories']])) 
{
            
return true
;
        
}

        $path 
$rootPath
;
        
$namePath ''
;

        
$i 1
;
        
$categories explode('/'$importData['categories']
);
        foreach (
$categories as $catName
{
            $namePath 
.= (empty($namePath) ? '' '/').strtolower($catName
);
            if (empty(
$cache[$namePath])) 
{

                $dispMode 
$this->_displayModes[2]
;

                
$cat Mage::getModel('catalog/category'
)
                    ->
setStoreId($store->getId
())
                    ->
setPath($path
)
                    ->
setName($catName
)
                    ->
setIsActive(1
)
                    ->
setIsAnchor(1
)
                    ->
setDisplayMode($dispMode
)
                    ->
save
();
                
$cache[$namePath] $cat
;
            
}
            $catId 
$cache[$namePath]->getId
();
            
$path .= '/'.$catId
;
            
$i
++;
        
}

        
return true
;
    
}

    
/**
     * Retrieve store object by code
     *
     * @param string $store
     * @return Mage_Core_Model_Store
     */
    
public function getStoreByCode($store
)
    
{
        $this
->_initStores
();
        if (isset(
$this->_stores[$store])) 
{
            
return $this->_stores[$store]
;
        
}
        
return false
;
    
}

    
/**
     *  Init stores
     *
     *  @param    none
     *  @return      void
     */
    
protected function _initStores 
()
    
{
        
if (is_null($this->_stores)) 
{
            $this
->_stores Mage::app()->getStores(truetrue
);
            foreach (
$this->_stores as $code => $store
{
                $this
->_storesIdCode[$store->getId()$code
;
            
}
        }
    }
}

?>

2.创建文件:app/code/local/Mycomp/Catalog/etc/config.xml

<?xml version="1.0"?>
<config
>
  <global>
        <
models
>
            <
catalog
>
                <
rewrite
>
                    <
convert_adapter_category>Mycomp_Catalog_Model_Convert_Adapter_Category</convert_adapter_category
>
                </
rewrite
>
            </
catalog
>
        </
models
>
  </global>
</
config>

3.创建文件:app/etc/modules/Mycomp_Catalog.xml

<?xml version="1.0"?>
<config
>
  <
modules
>
    <
Mycomp_Catalog
>
      <
codePool>local</codePool
>
      <
active>true</active
>
    </
Mycomp_Catalog
>
  </
modules
>
</
config>

 

4.创建文件:var/import/Categories.csv
"store",
"categories"
"default"
,
"Nifty"
"default"
,
"Nifty/Nifty Accessories"
"default"
,
"Nifty/Nifty Software"
"default"
,
"Nifty/Nifty Software/Windows"
"default"
,
"Nifty/Nifty Software/Linux"
"default"
,
"Nifty/Nifty Systems"
"default"
,"Nifty/Nifty Training and Support"

保存该profile,然后运行profile,就可导入目录了!

5. 进入Admin page > System > Import/Export > Advanced Profiles > Add New Profile
首先创建一个advanced profile,命名为“Import Categories”,然后添加XML如下:
<action type="dataflow/convert_adapter_io" method="load">
    <var 
name="type">file
</var>
    <var 
name="path">var/import
</var>
    <var 
name="filename"><![CDATA[Categories.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="
decimal_separator
"><![CDATA[.]]></var>
    <var name="
adapter
">catalog/convert_adapter_category</var>
    <var name="
method
">parse</var>
</action>

最新评论共有 位网友发表了评论
发表评论
用户名: 密码: 游客发言不需要密码