2013-03-01 63 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... 
}