Magento 1.5x add a new category programmatically and return category ID
I was wondering how to create new categories programmatic开发者_StackOverflowally in magento 1.5xx. Also I need to return the category ID back once its been created.
I wrote an initial post here which was completely wrong - it's not as easy to find the answer to this on Google as it should be!
Anyway, here's some code I'm using in Magento 1.5.0.1
-- The parent category you want to work with.
$parentCategory = $this->getParentCategory();
-- This method needs to refer to the static block you want to display if you're using one.
$categoryBlockId = $this->getCategoryBlockId();
$category = Mage::getModel( 'catalog/category' );
$category->setStoreId( $storeId );
$category->setName( $categoryName ); -- The name of the category
$category->setUrlKey( $categoryUrlkey ); -- The category's URL identifier
$category->setIsActive( 1 ); -- Is it enabled?
$category->setIsAnchor( 0 ); -- I think this relates to whether it shows in navigation.
-- Display mode can be 'PRODUCTS_AND_PAGE', 'PAGE', or (I think) 'PRODUCTS'
$category->setDisplayMode( $displayMode );
$category->setPath( $parentCategory->getPath() ); -- Important you get this right.
-- This is required if DisplayMode is 'PRODUCTS_AND_PAGE' or 'PAGE'.
$category->setLandingPage( $categoryBlockId );
$category->setPageTitle( 'Your Page Title' );
$category->save()
$categoryId = $category->getId();
There are other properties you can use as well (they correlate to the form fields when adding a category in the admin panel) but I don't have examples for them unfortunately. There are good posts out there online - I used them to create the above - but you might need to dig deeply via Google to find them.
I hope this helps and that you didn't read my first answer which wouldn't help.
Cheers, Zac
精彩评论