如何以编程方式将类别和类别路径添加到Magento?

时间:2014-03-05 14:46:09

标签: php magento

我想从一个magento商店导出类别并导入到另一个商店。

以下信息是必须在商店中插入的类别的一部分。

Default Category    2
All Categories  2/30
Electronics 2/30/12
TV & Video  2/30/12/13

我尝试以下脚本导入一个类别但不起作用。该脚本不会导入该类别。

require_once 'app/Mage.php';
Mage::app('default'); // Default or your store view name.

//get a new category object
$category = Mage::getModel('catalog/category');
$category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId().

//if update
if ($id) {
  $category->load($id);
}

$general['name'] = "All Categories";
$general['path'] = "2/30"; // catalog path
$general['description'] = "";
$general['meta_title'] = ""; //Page title
$general['meta_keywords'] = "";
$general['meta_description'] = "";
$general['landing_page'] = ""; //has to be created in advance, here comes id
$general['display_mode'] = "PRODUCTS_AND_PAGE"; //static block and the products are shown on the page
$general['is_active'] = 1;
$general['is_anchor'] = 0;
$general['page_layout'] = 'two_columns_left';

$category->addData($general);

try {
    $category->save();
    echo "Success! Id: ".$category->getId();
}
catch (Exception $e){
    echo $e->getMessage();
}

1 个答案:

答案 0 :(得分:1)

我复制了您的确切代码并进行了测试。你很难过,脚本是成功的,但magento后端没有类别......但是数据库中有一个,只需检查表 catalog_category_entity

问题是,您传递了错误的路径属性。最高类别是总体ID = 1。在安装之后,当您创建目录顶级类别时,它将大于1。在我的情况下,它是ID = 3.

因此,要在我创建的根类别(ID = 3)下方订购新类别,我必须为路径

设置值 1/3
$general['path'] = "1/3"; // catalog path

在你的情况下,我猜它应该使用以下值

$general['path'] = "1/2/30"; // catalog path