2013-03-01 60 views
0

我使用下面的代码添加类别。Magento类别列表为空

我的问题是:如何修改此代码以将类别添加到根类别?

require_once('../app/Mage.php'); 
Mage::app('mysite'); 

$category = Mage::getModel('catalog/category'); 
$category->setStoreId(Mage::app()->getStore()->getId()); 

if ($id) { 
    $category->load($id); 
} 
$general['name'] = "My Category"; 
$general['description'] = "Great My Category"; 
$general['meta_title'] = "My Category"; //Page title 
$general['meta_keywords'] = "My , Category"; 
$general['meta_description'] = "Some description to be found by meta search robots. 2"; 
$general['is_active'] = 1; 
$general['is_anchor'] = 0; 
$general['url_key'] = "cars";//url to be used for this category's page by magento. 
$category->addData($general); 

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

回答

1

只需补充一点:与路径

$general['path'] = "1/root_id/path_to_your_cat"; 

这一个,如果根类要实例add to is category id 5 use:

$general['path'] = "1/5"; 
+0

就是这样,谢谢! – 2013-03-01 17:17:04

+0

不客气。如果您不是将其创建为一次性脚本,请毫不犹豫地查看API方式。大量的文档可以通过互联网获得,安德鲁的信息应该会对你有所帮助。 – dagfr 2013-03-01 17:45:00

1

所以,很简单,如果你使用的API类:)

try { 
    $storeId = Mage::app()->getStore()->getId(); 
    $parentId = Mage::app()->getStore($storeId)->getRootCategoryId(); 
    $categoryData = array(
     'is_active'   => TRUE, 
     'default_sort_by' => 'price', 
     'available_sort_by' => 'price', 
     'include_in_menu' => TRUE, 
     'name'    => 'something here' 
    ); 
    $api = new Mage_Catalog_Model_Category_Api_V2; 
    return $api->create($parentId, (object) $categoryData, $storeId); 
} 
catch(Exception $e) { 
    echo $e->getMessage(); // do something here... 
}