How to show category siblings?
-
16-10-2019 - |
سؤال
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.
المحلول
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;?>
لا تنتمي إلى magento.stackexchange