Tuesday, March 22, 2011

How to automate related products selection in Magento?



This mod does the following
  • fetch all the products in a category
  • dispaly them below the main picture in product view
  • no need to set the related products (if your products are within the same category)
  1. go to app/design/frontend/your-package/your-theme/template/catalog/list/related.phtml ( make a backup of this one )
  2. overwrite all the codes in your related.phtml with this one
<?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<br />
$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; 
}
}
?>
Hope this helps! Godbless!

1 comment:

  1. Hi Christian!

    Which version is this for please? excellent thank you, but the url is not working in 1.6.1.

    Many thanks!

    ReplyDelete