Pretty similar to Alans answer, maybe a little less cycles:
$rootCategory = Mage::getModel('catalog/category') ->load(Mage::app()->getStore()->getRootCategoryId()); $sameStoreCategories = Mage::getResourceModel('catalog/category_collection') ->addIdFilter($product->getCategoryIds()) ->addFieldToFilter('path', array('like' => $rootCategory->getPath() . '/%')) ->getItems(); var_dump(array_keys($sameStoreCategories));
It will always work. It's ugly that you still need to load categories.
Here is an option you can use if tables with a flat category are included:
$sameStoreCategories = Mage::getResourceModel('catalog/category_flat_collection') ->addIdFilter($product->getCategoryIds()) ->getItems(); var_dump(array_keys($sameStoreCategories));
Why does it work? Because flat tables are indexed by the repository, and each flat table contains only entries for category entities that are associated with this category of store category roots.
So, despite the fact that you filter all category identifiers associated with the product, the collection will only contain categories that are present in the current store.
Vinai source share