Related products is one of the greatest up-sell features in Magento. However, the bad thing is that you need to assign related products for each product manually. It could be very time-consuming operation if you have a large product catalog.
In this article I'll show a solution, which will help to get automate related products from same category on product details page.
One of our clients asked me for an automatic way to show 4 random products from the same category on the product’s page.
I'll do it in 4 simple steps:
1. First of all, you need to create your own module in Magento. I'll call it Prashant_Relatedpro.
2. Then, in the config of your module you need to declare a block which will be used for auto-related products.
app/code/community/Prashant/Relatedpro/etc/config.xml:
<?xml version="1.0"?> <config> <modules> <Prashant_Relatedpro> <version>0.0.1</version> </Prashant_Relatedpro> </modules> <global> <blocks> <prashant_relatedpro> <class>Prashant_Relatedpro_Block</class> </prashant_relatedpro> </blocks> </global> </config>
3. Then, I need to create a block itself. In our case it will be located in app/code/community/Prashant/Relatedpro/Block/Related.php
Insert the following code in this file:
<?php class Prashant_Relatedpro_Block_Related extends Mage_Catalog_Block_Product_List_Related { protected function _prepareData() { $product = Mage::registry('product'); $product_id = $product->getId(); // get current product category if (Mage::registry('current_category')) { $category = Mage::registry('current_category'); } else { $catids = $product->getCategoryIds(); $cat_id = (int) array_pop($catids); if ($cat_id <= 0) return $this; $category = Mage::getModel("catalog/category")->load($cat_id); } if (! $category instanceof Mage_Catalog_Model_Category) return $this; $attributes = Mage::getSingleton('catalog/config') ->getProductAttributes(); $this->_itemCollection = Mage::getResourceModel('catalog/product_collection')->addAttributeToSelect($attributes) ->addCategoryFilter($category) ->addStoreFilter() ->setPageSize(6) // display 6 related products ->setCurPage(1) ->addIdFilter(array($product_id), true); $this->_itemCollection->getSelect()->orderRand(); if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) { Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection, Mage::getSingleton('checkout/session')->getQuoteId() ); $this->_addProductAttributesAndPrices($this->_itemCollection); } Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection); $this->_itemCollection->load(); foreach ($this->_itemCollection as $product) { $product->setDoNotUseCategoryId(true); } return $this; } }
4. Finally, you need to open layout file app/design/frontend/{your package}/{your theme}/layout/catalog.xml
and replace this block
<block type="catalog/product_list_related" name="catalog.product.related" before="-" template="catalog/product/list/related.phtml"/>
with this one:
<block type="prashant_relatedpro/related" name="catalog.product.related" as="related_products" after="-" template="catalog/product/list/related.phtml"/>
Automatic Related Products is an ideal solution to cross-sells and cross-promotion. With my custom module, you can easily show related products from same category on product details page.
Your customer will get more your products by recommendations for every product in minutes.
I hope this helps someone. If you have any suggestions for improvement or code upgrades/fixes please let me know in the comments.
Nice article but we need to configure the module in app/etc/modules/Prashant_Relatedpro.xml
Thanks,
kiran