nileco-logo-small-text5
  • HOME
  • PROJECTS
  • SERVICES
  • ABOUT
    • WHO WE ARE
    • TESTIMONIALS
  • MAGENTO BLOG

How to Automatically add Related Products in Magento

Magento How ToJune 14, 2014CJ

Adding appropriate Related Products to your Magento Products can be a time consuming and daunting task.  The following tutorial will explain how to automate this process.  After making these changes Magento will choose products from the same category and place them in the Related Products section of your Magento.

  1. Browse to the following file:
    app/design/frontend/default/YOUR THEME/template/catalog/list/related.phtml
  2. Replace the code in this file with the following:
    <?php
    $_product = $this->getProduct();
    if ($_product) {
     // get collection of categories this product is associated with
     $categories = $_product->getCategoryCollection()
     ->setPage(1, 1)
     ->addFieldToFilter('parent_id',"2")
     ->load();
     // if the product is associated with any category
     if ($categories->count())
     foreach ($categories as $_category){
     $cur_category = Mage::getModel('catalog/category')->load($_category->getId());
     $prodCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($_category);
     Mage::getSingleton('catalog/product_status')
     ->addVisibleFilterToCollection($prodCollection);
     Mage::getSingleton('catalog/product_visibility')
     ->addVisibleInCatalogFilterToCollection($prodCollection);
     if($prodCollection->count() > 1) :
     ?><
     div class="related-product">
     <div class="block-title">
     <h4><?php echo $this->__('More from this artist...') ?></h4>
     </div>
     <?php $products = Mage::getResourceModel('catalog/product_collection')
     ->addCategoryFilter($_category)
     ->addAttributeToSelect('small_image'); ?>
     <ol class="mini-products-list" id="block-related">
     <div class="block-content">
     <?php foreach ( $products as $productModel ){
     $_product = Mage::getModel('catalog/product')->load($productModel->getId());
     $width=100; $height=100;
     $_imageUrl = $this->helper('catalog/image')->init($productModel, 'small_image')->resize($width, $height);
     $currentUrl = $this->helper('core/url')->getCurrentUrl(); //SPLIT THE URL FOR QUERY STRING
     $rel_product = explode( "?", $currentUrl);
     //SPLIT THE URL FOR CATEGORY
     $cprod_url = explode( "/", $_product->getProductUrl()); //ASSIGN THE STRIPPED URL TO A VARIABLE
     $isyan = $cprod_url[0].'//'.$cprod_url[2].'/'.$cprod_url[4]; //WE WILL HIDE THE PRODUCT THAT IS CURRENTLY BEING VIEWED FROM DISPLAYING ON THE RELATED PRODUCTS
     if( $_product->getProductUrl() != $rel_product[0] && $isyan != $rel_product[0]){
     ?>
     <li class="item">
     <a href="<?php echo $isyan ?>" class="product-image" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><img src=<?=$_imageUrl ?> width="<?=$width?>" height="<?=$height?>"/></a>
     </li>
     <?php }
     }
     ?>
     </div>
     </ol>
     </div>
    <?php endif;
    }
    }
    ?>
Previous post Create an automated navigation menu of all CMS pages in Magento Next post How to Add Free Shipping Icon to Magento Product Page

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2016 All rights reserved. nileco