Compare products from same category in Magento 1.9

Compare products from same category in Magento 1.9

October 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Compare products from same category in Magento 1.9

Solving Category-based Product Comparison in Magento 1.9

Hello developers! Today we’re going to tackle an interesting challenge in Magento CE 1.9 - restricting product comparisons based on the product category.

The Scenario: Unrelated Products Can Be Compared Currently

In the current setup, any two products are comparable regardless of their categories. For a health products e-commerce store, this means a wheelchair can be compared with a Glucometer or other unrelated items. This leads to irrelevant and confusing comparisons for customers.

The Goal: Only Allow Comparisons Within Same Category

The aim is to modify the comparison process so that only products within the same category are listed as comparable options. This will enhance user experience by providing meaningful product comparisons.

Step 1: Create a New Extension Module

First, we need to create a new local extension module called mydir. This will house our custom changes.

Create the following files and directories:

app/etc/modules
└── mydir_All.xml

app/code/local/mydir
├── controllers
│   └── Product
│       └── CompareController.php
└── etc
    └── config.xml

Step 2: Update mydir_All.xml

Open the mydir_All.xml file and add the following content:

<?xml version="1.0"?>
<config>
    <modules>
        <mydir>
            <active>true</active>
            <codePool>local</codePool>
        </mydir>
    </modules>
</config>

This ensures our module is active and loaded from the local code pool.

Step 3: Create CompareController.php

In app/code/local/mydir/controllers/Product/CompareController.php, let’s add a new method called addAction():

class MyDir_Product_CompareController extends Mage_Catalog_Product_CompareController
{
    public function addAction()
    {
        $productId = (int) $this->getRequest()->getParam('product');

        if (!$productId) {
            $this->_redirect('');
        }

        $originalProductId = (int)$this->getRequest()->getParam('original_product');
        $categoryId = Mage::registry('current_category')->getId();

        // Get all products in the current category
        $productsInCategory = Mage::getModel('catalog/product')
            ->getCollection()
            ->addCategoryFilter($categoryId);

        if (!count($productsInCategory)) {
            $this->_redirect('');
        }

        // Check if the original product is in the same category as the new product
        $originalProduct = Mage::getModel('catalog/product')->load($originalProductId);
        if ($originalProduct->getCategoryIds() && !in_array($categoryId, $originalProduct->getCategoryIds())) {
            $this->_redirect('');
        }

        parent::addAction();
    }
}

This code will redirect the user away from the comparison process if:

  1. The product ID is missing or invalid.
  2. There are no products in the current category.
  3. The original product being compared isn’t in the same category as the new product.

Step 4: Update config.xml

In app/code/local/mydir/etc/config.xml, add the following content:

<?xml version="1.0"?>
<config>
    <modules>
        <mydir>
            <version>0.1.0</version>
        </mydir>
    </modules>
    <frontend>
        <routers>
            <tag>
                <args>
                    <modules>
                        <mydir before="Mage_Catalog">mydir</mydir>
                    </modules>
                </args>
            </tag>
        </routers>
    </frontend>
</config>

This updates the front-end router to use our custom CompareController instead of Magento’s default one.

The Result: Category-based Product Comparisons

With these changes, the product comparison process is now restricted to products within the same category. Customers will only see relevant products for comparison based on the currently selected category.

Conclusion and Further Reading

In this post, we demonstrated how to modify the built-in Magento 1.9 functionality to create a custom extension that enforces category-based product comparisons.

Remember, working with Magento modules requires careful testing in your local development environment before deploying changes live. Always have backups of your database and codebase ready for rollbacks if needed.

For more tips on Magento module development, check out this detailed guide from Laravel Company. It provides step-by-step instructions along with real-world examples.

Happy coding! Let me know in the comments if you have any questions or need further clarification on any of these steps.

Magento 1 Catalog API Extensions

Our network