How to get the category ids from particular website in magento? Product is assigned to both website I am fetching product collection by using code:


and then get product category by:

foreach ($_productCollection as $_product){
    $_categories = $_product->getCategoryIds();

but I am getting categories from both websites which is not correct. I have tried:


but it is not working


As noted in a comment above, a clever approach was posted by Vinai on StackOverflow. It is much preferable to the direct query of the index table which would be subject to breaking via configuration.

<!-- language: php -->
  $rootCategory = Mage::getModel('catalog/category')

$sameStoreCategories = Mage::getResourceModel('catalog/category_collection')
    ->addFieldToFilter('path', array('like' => $rootCategory->getPath() . '/%'))


This will always work. The ugly thing is that you still need to load the categories.

Here is a variation you can use if the flat category tables are enabled:

$sameStoreCategories = Mage::getResourceModel('catalog/category_flat_collection')


Why does it work? Because the flat tables are indexed by store, and each flat table only contains the category entity records that are associated with that store groups root category.

So even though you are filtering by all category IDs associated with the product, the collection will only contain the categories present in the current store.

| improve this answer | |

I have solve this issue. I have get categoryids by below steps. I have made one helper function catalog/category.

$storeId = Mage::app()->getStore()->getStoreId();
$storeids = $_product->getStoreIds();
   if(count($storeids) > 1)
    $_categories = $this->helper('catalog/category')->getCategoryOfStore($storeId,$_product->getId());
   $_categories = $_product->getCategoryIds();

helper function

class Supplycity_Catalog_Helper_Category extends Mage_Catalog_Helper_Category
    public function getCategoryOfStore($store,$prodoctid)
      $connection = Mage::getSingleton('core/resource')->getConnection('core_read');

    $select = $connection->select()
        ->from('catalog_category_product_index', array('category_id')) 

    $rowsArray = $connection->fetchAll($select); 
    return $rowsArray;


| improve this answer | |

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.