3

I have been expanding my catalog and have run into a few suppliers that are carrying the exact same products. The only difference is the price and stock levels.

To avoid duplicate products on my website (and search engine penalties), I've created a Grouped Product with the name of the product and have added the 2 identical products from the 2 different suppliers and have set them to "Not visible individually"

The problem I now face is that by setting the Associated Products to "Not visible individually" is that the link becomes dead.

What I would like to do is to automatically create a redirect to the Grouped Product for all Associated Products

How do you create redirects of Associated Products to the Grouped Product ?

1 Answer 1

4

The idea is this. When viewing a product page check to see if the product belongs to a grouped product. If so, redirect to the main product page.
There is a catch. A simple product can be part of more than one grouped product. In this case the simplest approach is to redirect to the first one found.

Here is some code you can use.
Create an extension. Let's call it StackExchange_Grouped.

app/etc/modules/StackExchange_Grouped.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_Grouped>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Catalog />
            </depends>
        </StackExchange_Grouped>
    </modules>
</config>

app/code/local/StackExchange/Grouped/etc/config.xml - the module configuration file where you declare an observer for the product view page

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_Grouped>
            <version>1.0.0</version>
        </StackExchange_Grouped>
    </modules>
    <global>
        <models>
            <stackexchange_grouped>
                <class>StackExchange_Grouped_Model</class>
            </stackexchange_grouped>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch_catalog_product_view>
                <observers>
                    <stackexchange_grouped>
                        <class>stackexchange_grouped/observer</class>
                        <method>redirectToParent</method>
                    </stackexchange_grouped>
                </observers>
            </controller_action_predispatch_catalog_product_view>
        </events>
    </frontend>
</config>

app/code/local/StackExchange/Grouped/Model/Observer.php - the observer that should redirect to the grouped product page.

<?php
class StackExchange_Grouped_Model_Observer
{
    public function redirectToParent($observer)
    {
        $id = Mage::app()->getRequest()->getParam('id');
        $groupedParentIds = Mage::getResourceSingleton('catalog/product_link')
            ->getParentIdsByChild($id,
                Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED);
        if (isset($groupedParentIds[0])) {
            $mainProduct = Mage::getModel('catalog/product')
                ->setStoreId(Mage::app()->getStore()->getId())->load($groupedParentIds[0]);
            Mage::app()->getResponse()->setRedirect($mainProduct->getProductUrl(), 301);
            Mage::app()->getResponse()->sendResponse();
            exit;
        }
        return $this;
    }
}

Clear the cache and give it a go.

2
  • wow thanks for the quick reply Marius! Do I need to keep the products visible or I can leave them "not visible individually" ?
    – SR_Magento
    Commented May 13, 2015 at 12:42
  • I'm not sure. I think you can set them invisible because the event is dispatched before the visibility is checked. On the other hand, if the products are not visible I'm not sure the url rewrites are generated for them (but not sure). Test with both visible and not visible and see what comes up.
    – Marius
    Commented May 13, 2015 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.