I need to display the categories based on the website. Somebody help me How to get categories based on store or website?
3 Answers
you will get by this root category. the stores are related with the root category.in admin panel
you can see in manage store
.
$storeid=Mage::app()->getStore()->getStoreId();
$rootid = Mage::app()->getStore($storeid)->getRootCategoryId();
$categories = Mage::getModel('catalog/category')
->getCollection()
->addFieldToFilter('path', array('like'=> "1/$rootid/%"));
Get store specific categories
The following code will fetch all active store specific categories. The following helper function does so:-
getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)
$helper = Mage::helper('catalog/category');
// sorted by name, fetched as collection
$categoriesCollection = $helper->getStoreCategories('name', true, false);
// sorted by name, fetched as array
$categoriesArray = $helper->getStoreCategories('name', false, false);
Hope this helps. Thanks.
if you want the categories for specific store id:
$allCategories = Mage::getModel ( 'catalog/category' )->setStoreId(7);
$categoryTree = $allCategories->getTreeModel();
$categoryIds = $categoryTree->getCollection()->getAllIds();
if further want to get its data save in csv file:
if ($categoryIds) {
$outputFile = "var/export/categories-and-ids.csv";
$write = fopen($outputFile, 'w');
foreach ( $categoryIds as $categoryId ) {
$parentId = $allCategories->load($categoryId)->getParentId();
$data = array($parentId, $allCategories->load($categoryId)->getName(), $categoryId);
fputcsv($write, $data);
}
}
fclose($write);