Reputation: 3825
What I need is to add new categories and subcategories from .csv file. I can create new root category, but I don't know how to make subcategory:
$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 file will be formatted like this:
root_cat_id;subcat_id;subcat_name;subsubcat_id;subsubcat_name;
ex.
1;2;Animal;3;Dog;
How can I get id of just added category, and add subcategory related to this category?
Thanks in advance
Upvotes: 0
Views: 3111
Reputation: 14182
To make a category the child of another category, set the path to the parent categories path before saving it.
$childCategory->setPath($parentCategory->getPath())
After the child category is saved (and thus has an ID), the ID will be appended to the path attribute automatically after save()
is called.
Magento will also automatically set the parent_id and level attributes for you.
Upvotes: 2