Search code examples
phpmagentomagento-1.8

Magento get category collection for product - multi site specific


I have a multi site setup, Magento 1.8, and I want to list the categories a product is contained in within the file template/catalog/product/view/description.phtml

I want to do this on a per site basis. For example, Product is for sale on Site 1 and Site 2...

On Site 1 it is listed in category A and Category B

On Site 2 it is listed in category Y and Category Z

So in the Site 1 product description I want to list category A and B, but not Y and Z

And in the site 2 product description I want to list category Y and Z but not A and B.

This is the code I have so far, but this lists all categories the product is included in from all sites, how can I force it to only list categories from a specific site...

$catIds = $this->getProduct()->getCategoryIds();
$catCollection = Mage::getResourceModel('catalog/category_collection')
                     ->addAttributeToSelect('name')
                     ->addAttributeToSelect('url')
                     ->addAttributeToSelect('*')
                     ->addAttributeToFilter('entity_id', $catIds)
                     ->addIsActiveFilter();

foreach($catCollection as $cat){

    echo "<a href=\"";
    echo $cat->getUrl();
    echo "\">";
    echo $cat->getName();
    echo "</a><br/>";
}

Solution

  • Try this:

    $category_ids = $this->getProduct()->getCategoryIds();
    foreach ($category_ids as $category_id) {
        $category = Mage::getModel('catalog/category')->load($category_id);
        if ($category->getIsActive()) {
            echo $category->getUrl();
            echo $category->getName();
        }
    }
    

    Or more in line with your original code (and less work for your database), you could try this filter:

    ->addFieldToFilter('is_active', 1)