2012-07-04 70 views
0

我需要的是从.csv文件添加新的类别和子类别。我可以创建新的根类,但我不知道如何使子类别:添加新的类别和子类别 - Magento

$category = Mage::getModel('catalog/category'); 
$category->setStoreId(0); 

    $rootCategory['name'] = 'Reserved'; 
    $rootCategory['path'] = "1"; // for root category 
    $rootCategory['display_mode'] = "PRODUCTS_AND_PAGE"; 
    $rootCategory['is_active'] = 1; 

    $category->addData($rootCategory); 
$parentCategory= $category; // doesn't work, but i want here to get this (root) category id 
    try { 
    $category->save(); 
    } 
    catch (Exception $e){ 
    echo $e->getMessage(); 
    } 

CSV文件将被格式化是这样的:

root_cat_id;subcat_id;subcat_name;subsubcat_id;subsubcat_name; 
ex. 
1;2;Animal;3;Dog; 

我怎么能得到的只是添加类的ID,并添加与此类别相关的子类别?

在此先感谢

回答

1

为了使一个类别中的另一类别的孩子,保存之前设定的路径,父类路径。

$childCategory->setPath($parentCategory->getPath()) 

子类别保存后(并因此具有一个ID),该ID将被追加到路径save()称为后自动属性。
Magento也会自动为你设置parent_id和level属性。