2013-03-01 2 views
0

아래의 코드를 사용하여 카테고리를 추가하고 있습니다.Magento 카테고리 목록이 비어 있습니다.

내 질문은 :이 코드를 수정하여 루트 범주에 범주를 추가하는 방법은 무엇입니까? 당신은 API 클래스를 사용하는 경우

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"; 

를, 예를 들어 루트 카테고리 당신이 원하는 경우 에 추가 카테고리 ID 5 사용 :

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

감사합니다. –

+0

당신을 진심으로 환영합니다. 이 스크립트를 원샷 스크립트로 만들지 않으려면 API 방법을 주저하지 마십시오. 많은 문서가 인터넷을 통해 제공되며 Andrew의 메시지가 도움이 될 것입니다. – dagfr

1

그것은

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... 
} 
관련 문제