简体   繁体   English

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

[英]How to add a category and category path to Magento programatically?

I'd like to export categories from one magento store and import to another one. 我想从一家magento商店中导出类别,然后导入到另一家商店中。

The following information is part of categories which must to inserted in store. 以下信息是必须在商店中插入的类别的一部分。

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

I try this following script to import one category but doesn't work. 我尝试使用以下脚本导入一个类别,但不起作用。 The script doesn't import the category. 该脚本不会导入类别。

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

I copied your exact code and tested it. 我复制了您的确切代码并对其进行了测试。 As you sad, script is sucessfull but no category in magento backend ... but there is one in the database, just check the table catalog_category_entity . 令人难过的是,脚本已成功完成,但在magento后端中没有类别...但是数据库中有一个类别,只需检查表catalog_category_entity即可

The problem is, you pass the wrong path property. 问题是,您传递了错误的path属性。 The top category is allways ID=1. 顶部类别始终是ID = 1。 When, after installation, you create your catalogs top category, it will be something greater then 1. In my case it is ID=3. 在安装后,当您创建目录的顶级类别时,它将大于1。在我的情况下,它是ID = 3。

So to order the new category below the root category I created (ID=3), I must set the value 1/3 for path 因此,要在我创建的根类别(ID = 3)以下订购新类别,我必须将path的值设置为1/3

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

In your case I gues it should work with the following value 以您的情况,我认为它应具有以下值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM