-1

In product details page we can get related products easily. But I want to show those related products in category wise.

4
  • What do you mean by "category wise"? Grouped by category, one some categories shown or something else. Jul 19, 2014 at 8:05
  • Like Category1 Product1 Product2 Category2 Product3 Product4, where Product1, Product2, Product3, Product4 are related products. But, I want to show those their category wise(like Category1, Category2)
    – lmnmamun
    Jul 19, 2014 at 8:21
  • so what have you tried so far? Jul 19, 2014 at 9:46
  • 1
    Related products are tied to a product, not to a category. You can get all the related products from a category, but that will take you some time and resources. I added a solution in my answer which might be the correct one for you.
    – mbalparda
    Jul 19, 2014 at 13:53

2 Answers 2

1

A simple solution for this is doing a "featured" attribute per product and show only the product with that attribute in the frontend, filtered by the current category. This is not a core feature of Magento, but it can be easily achieved with something like this:

$_category  = $this->getCurrentCategory();
$current_level = $_category->getLevel(); 
$collection = $_category->getCollection(); 

$collection->addAttributeToSelect('url_key')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('parent_id')
            ->addAttributeToFilter('featured', 1)
            ->addAttributeToFilter('level',array('in'=>array($current_level+1,$current_level+2)))
            ->setOrder('position','ASC')
            ->load();

Source here.

Guide to create attributes here.

0
<?php
$related = Mage::registry('current_product')->getRelatedProductIds();
$categoryShow = 2; // id of category wise
foreach ($related as $productId) {
    $product = Mage::getModel('catalog/product')->load($productId);
    $categoryIds = $product->getCategoryIds();
    foreach ($categoryIds as $categoryId) {
        /*
         * if category wise id is dinamic
         * $catefory = Mage::getModel('catalog/product')->load($categoryId);
         * change the 'if' if ($category->getName() == 'wise')
         * 
         */
        if ($categoryId == $categoryShow) {
            // show the product
        }           
    }
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.