3

I have a Magento based website. In my product page I need to show related categories regarding the category the product fall into.

For example suppose the category structure A- has three child categories B,C and D. Where C has a product than on this product page i need to show B and D as a related category.

Is there is any way to achieve this.

Thanks in advance.

  • is the tag ecomdev-phpunit by purpose? Are you trying to do this during an unit test? – Alex Mar 12 '13 at 15:27
5

Let's say that you already have the current category in the variable $category. This piece of code should give you all the (active) category siblings.

$siblings = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
$siblings->addAttributeToSelect('*')
        ->addAttributeToFilter('parent_id', $category->getParentId()) //siblings have the same parent as the current category
        ->addAttributeToFilter('is_active', 1)//get only active categories
        ->addAttributeToFilter('entity_id', array('neq'=>$category->getId()))//exclude current category
        ->addAttributeToSort('position');//sort by position

Now you can loop through the siblings and list them:

<?php foreach ($siblings as $sibling): ?>
    <a href="<?php echo $sibling->getUrl();?>"><?php echo $sibling->getName();?></a><br />
<?php endforeach;?>
  • Thanks Marius your code works like a charm, thanks for the great help. – Sarvagya Mar 13 '13 at 10:07

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.