在导入csv

时间:2016-04-21 12:18:41

标签: php magento csv

我正在从旧数据库构建一个新的mage站点。现在,我正在使用我在网上找到的脚本导入类别csv。它工作正常,但我注意到类别ID增加,而不是真正使用他们在旧网站上的类别ID,这在导入产品时会很麻烦。如果这是可行的或任何更好的方法,请帮助甚至只是启示。非常感谢你。 我尝试添加:

$catID = $importData['cat_id']; and 
$cat->setID($catID); (it didnt work) 

以下是代码:

    <?php
class ImpCat_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');
        $batchImportModel = $batchModel->getBatchImportModel();
        $importIds        = $batchImportModel->getIdCollection();
        foreach ($importIds as $importId) {
            $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']);
        /*$IsActive = $importData['IsActive'];*/
        $IsActive        = $importData['is_active'];
        $IsAnchor        = $importData['is_anchor'];
        $Description     = $importData['description'];
        $IncludeInMenu   = $importData['include_in_menu'];
        $MetaTitle       = $importData['meta_title'];
        $MetaKeywords    = $importData['meta_keywords'];
        $MetaDescription = $importData['meta_description'];
        $Image           = $importData['image'];
        $URlkey          = $importData['url_key'];
        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($IsActive)->setIsAnchor($IsAnchor)->setDisplayMode($dispMode)->save();
                $cat      = Mage::getModel('catalog/category')->load($cat->getId());
                $cat->setIncludeInMenu($IncludeInMenu);
                $cat->setDescription($Description);
                $cat->setMetaTitle($MetaTitle) . $cat->setMetaKeywords($MetaKeywords);
                $cat->setMetaDescription($MetaDescription);
                $cat->save();
                $cat                      = Mage::getModel('catalog/category')->load($cat->getId());
                $data['meta_keywords']    = $MetaKeywords;
                $data['meta_title']       = $MetaTitle;
                $data['meta_keywords']    = $MetaKeywords;
                $data['meta_description'] = $MetaDescription;
                $data['url_key']          = $URlkey;
                $cat->addData($data);
                $cat->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(true, true);
            foreach ($this->_stores as $code => $store) {
                $this->_storesIdCode[$store->getId()] = $code;
            }
        }
    }
}

这是我导入的示例csv

table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
    text-align: left;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<h2>Sample CSV to import</h2>

<table style="width:100%">
  <tr>
    <th>store</th>
    <th>categories</th>
    <th>cat_id</th>
    <th>is_active</th>
    <th>meta_title</th>
    <th>meta_keywords</th>
    <th>meta_description</th>
    <th>include_in_menu</th>
    <th>is_anchor</th>
    <th>description</th>
  </tr>
  <tr>
    <td>default</td>
    <td>/Supplies</td>
    <td>65</td>
    <td>1</td>
    <td></td>
    <td></td>
    <td>1</td>
    <td>1</td>
    <td></td>
  </tr>
</table>
</body>
</html>

0 个答案:

没有答案