Redirect Simple Products to Configurable Parents with Options Preselected

Recently I met quite a challenge while building an extension for Magento. By default, the urls of simple child products whom belong to a configurable parent are answered with a 404. This behaviour can be easily overridden with a small extension.

Our client wanted to take it a step further. Today we will build an extension that redirects URLs of simple products to their configurable parents with options preselected.

If you’re looking for a tutorial on how to redirect requests to simple product pages to their configurable parent with swatches preselected in Magento 2, please continue reading here.

The XML Basics

Before we start, we need to create the basic files needed for any Magento extension.

Create app/etc/modules/DaanvandenBergh_RedirectSimpleProducts.xml and add the following snippet into it:

<?xml version="1.0"?>
<Mage_Catalog />

Then create app/code/local/DaanvandenBergh/RedirectSimpleProducts/etc/config.xml and paste the following code into it:

<?xml version="1.0"?>
view raw config.xml hosted with ❤ by GitHub

Now we have everything in place for our extension to be recognized by Magento as active. Also, we’ve told Magento which classes we will be using and when our methods should be fired.

We’ve added an observer to the event when a user opens a catalog product view and provided the class and method that should be executed.

So what does this mean in regular English? It means that this extension waits for a user to open a product page and then triggers the method we provided.

Now let’s move on to where the magic happens.

Redirect Simple Product to Configurable Parent

In config.xml we specify when our extension should be triggered. In the class below we will specify what our extension should do.

Create /app/code/local/DaanvandenBergh/RedirectSimpleProducts/Model/Observer.php and add the following code:

class DaanvandenBergh_RedirectSimpleProducts_Model_Observer {
public function redirectToConfigurable( $observer ) {
$id = Mage::app()->getRequest()->getParam( 'id' );
$configurableParentIds = Mage::getResourceSingleton( 'catalog/product_type_configurable' )->getParentIdsByChild( $id );
if ( isset( $configurableParentIds[0] ) ) {
$mainProduct = Mage::getModel( 'catalog/product' )
->setStoreId( Mage::app()->getStore()->getId() )
->load( $configurableParentIds[0] );
$productAttributeOptions = $mainProduct->getTypeInstance( true )->getConfigurableAttributesAsArray( $mainProduct );
$attributeOptions = array();
foreach ( $productAttributeOptions as $productAttribute ) {
$simpleProduct = Mage::getModel( 'catalog/product' )->getCollection()
->addAttributeToFilter( 'entity_id', array( 'eq' => $id ) )
->addAttributeToSelect( $productAttribute['attribute_code'] )
->setPageSize( 1 )
$attributeOptions[ $productAttribute['attribute_id'] ] = $simpleProduct->getData( $productAttribute['attribute_code'] );
Mage::getSingleton( 'core/session' )->setSuperAttributes( $attributeOptions );
Mage::app()->getResponse()->setRedirect( $mainProduct->getProductUrl(), 301 );
return $this;
view raw Observer.php hosted with ❤ by GitHub

That’s it! Our method detects the product ID of the visited Simple Product URL and uses that to detect the ID of its configurable parent. Then it creates an array of all the available options, filters out the ones that are used by the Simple Child Product and sends it along with a 301-redirect.

The biggest upside of this approach is its SEO Friendliness. Since it uses the actual URL of the configurable parent, it doesn’t create any duplicate content. This makes this approach preferable over the approach of some premium plugins.

I hope this extension helps you. It redirects simple product URLs that are not visible in the catalog/search to their configurable parent with its options preselected. If you have any remarks or questions. Please, leave a comment!