问题
I am trying to get a list of category according to store_id , but all my tries have failed
ive tried
$categories = Mage::getModel('catalog/category')
->getCollection()
->setStoreId(21)
->addAttributeToSelect('*');
but it gives me all the categories from all the stores, i have tried
->addFieldToFilter('store_id', '21')
and ->addStoreFilter(21)
but with no luck , any help or suggestions will be greatly appreciated, thanking you in advance
回答1:
Categories in magento doesn't have to Store relations ( because its used by stores ( store can point to subcategory as its root category for instance ) etc.. ) So any category you create it will be visible in all stores.
But is has attribute is_active ( which is store view scope ).
So to get the categories in specific store ( you need to make sure its not active in other stores )
and Filter with the attribute
->addFieldToFilter('is_active', 1)
Hope this help you.
回答2:
This works in 1.9
$storeId = Mage::app()->getStore()->getStoreId();
$rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
$categories = Mage::getModel('catalog/category')
->getCollection()
->setStoreId($storeId)
->addFieldToFilter('is_active', 1)
->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))
->addAttributeToSelect('*');
回答3:
Try this:
$storeId = 21;
$rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCollection();
$categories->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"));
回答4:
$storeId=21;
->addFieldToFilter('store_id',$storeId)
来源:https://stackoverflow.com/questions/16743143/how-to-get-category-list-according-to-store-id-in-magento