Automatic Related Products

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.