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.