0

How do i set a new category for all the stores in a website?

I've created a new root category via my custom module, and need to make this as the root category for all the stores in a particular website?

I would like the newer stores created also to have the same root category.

2

The root category is set at the store level. So all store views in a store have the same root category. To set that manually, just edit the store in System->Manage Stores and select it from the Root Category dropdown.
note: the category must be a top level one.
To set it by code do this:

$websiteId = '1';//change it if it's different
Mage::getModel('core/website')->load($websiteId);
$stores = $website->getGroupCollection();
$rootCategory = 12;//change the id of the category here
foreach ($stores as $store){
    $store->setRootCategoryId($rootCategory);
    $store->save();
}

There is no 'out of the box' way of making sure that any other store added to the website will have the same root category since the possibility of having different root categories for different stores is a feature of Magento. Maybe you can add an observer on the store_group_save_after event and setting. Something like this:

public function observeStoreGroup($observer){
    $group = $observer->getEvent()->getStoreGroup();
    if ($group->isObjectNew()){ //check if the store is being created.
        $group->->setRootCategoryId($rootCategory);//use the same $rootCategory as above
    }
}
0
    $website    = Mage::app()->getWebsite($webSiteId);
    $storeGroup = $website->getGroupCollection();

    foreach ($storeGroup as $groupModel) {
        $groupModel->setRootCategoryId($newRootCategoryId)->save();
        Mage::dispatchEvent('store_group_save', array('group' => $groupModel));
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.