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 we will show a solution, which will help to automate related products.
Solution 1. Ready to use module.
First of all, if you need a flexible solution for automatic related products, you can use commercial AW extension "Automatic Related Products".
Solution 2. Customize your Magento yourself.
Our custom solution use rather simple approach: the Related Products will be selected from the same category as the current product.
We will do it in 4 simple steps:
1. First of all, you need to create your own module in Magento. We will call it Turnkeye_Autorelated.
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/Turnkeye/Autorelated/etc/config.xml:
<?xml version="1.0"?> <config> <modules> <Turnkeye_Autorelated> <version>0.0.1</version> </Turnkeye_Autorelated> </modules> <global> <blocks> <turnkeye_autorelated> <class>Turnkeye_Autorelated_Block</class> </turnkeye_autorelated> </blocks> </global> </config>
3. Then, we need to create a block itself. In our case it will be located in app/code/community/Turnkeye/Autorelated/Block/Related.php
Insert the following code in this file:
<?php class Turnkeye_Autorelated_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; } }
As you see we hard-coded amount of the product displayed. This is really very simple approach, you might need to implement more flexible way to set number of products displayed.
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="turnkeye_autorelated/related" name="catalog.product.related" as="related_products" after="-" template="catalog/product/list/related.phtml"/>
If you did everything right, you will see the automatic related products block in the right column of all product details pages in the store.
1. I don't have the file app/design/frontend/{your package}/{your theme}/layout/catalog.xml in my theme
2. I would like to use this block instead of a static block I have in the theme that replace related if not present
how can I recall your block instead?
If yes, please contact me and tell me how much it can cost. Thanks
Yes, looks like it is the reason of the issue.
@ Kate
Yes it is possible in Magento, we can develop such module for you if you need it. Feel free to contact our team.
I followed the steps above but nothing happened. Could it be because the right column is not in use in our theme? What is the solution?
Thanks,
Iain
Can i create duplicate related product, duplicate one will be replacement product based on category.
I have been facing a problem with this issue.If possible please assist me or any guideline.
Anwar
Yes, the Related Products will be selected from the same category as the current product.
Does this pull products from the same category of the current product page ?
If yes, then VOILA!!