1

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:

$_productCollection=$this->getLoadedProductCollection();

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:

$_productCollection=$this->getLoadedProductCollection()->setStoreId($store_id);

but it is not working

2

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')
    ->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));

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')
    ->addIdFilter($product->getCategoryIds())
    ->getItems();

var_dump(array_keys($sameStoreCategories));

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.

-2

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());
   else
   $_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')) 
        ->where('product_id=?',$prodoctid)              
        ->where('store_id=?',$store);          

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

Thanks

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.