Php 在导入csv中包含类别ID
我正在从旧数据库建立一个新的mage站点。现在,我正在使用在线找到的脚本导入一个类别csv。它工作得很好,但我注意到类别ID增加了,并且没有真正使用旧站点上的类别ID,这在导入产品时会很麻烦。如果这是可行的或任何更好的方法,请提供帮助。非常感谢你。 我试着加上:Php 在导入csv中包含类别ID,php,magento,csv,Php,Magento,Csv,我正在从旧数据库建立一个新的mage站点。现在,我正在使用在线找到的脚本导入一个类别csv。它工作得很好,但我注意到类别ID增加了,并且没有真正使用旧站点上的类别ID,这在导入产品时会很麻烦。如果这是可行的或任何更好的方法,请提供帮助。非常感谢你。 我试着加上: $catID = $importData['cat_id']; and $cat->setID($catID); (it didnt work) 代码如下: <?php class ImpCat_Catalog
$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
商店
类别
猫咪
你是活跃的吗
元标题
meta_关键字
元描述
在菜单中包含
你是锚吗
描述
违约
/补给
65
1.
1.
1.