in , ,

Redirect Simple Products to Configurable Parents with Options Preselected

Swatches - Redirect Simple Product to Configurable Product

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"?>
<config>
<modules>
<DaanvandenBergh_RedirectSimpleProducts>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_Catalog />
</depends>
</DaanvandenBergh_RedirectSimpleProducts>
</modules>
</config>

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

<?xml version="1.0"?>
<config>
<modules>
<DaanvandenBergh_RedirectSimpleProducts>
<version>1.0.0</version>
</DaanvandenBergh_RedirectSimpleProducts>
</modules>
<global>
<models>
<daanvandenbergh_redirectsimpleproducts>
<class>DaanvandenBergh_RedirectSimpleProducts_Model</class>
</daanvandenbergh_redirectsimpleproducts>
</models>
</global>
<frontend>
<events>
<controller_action_predispatch_catalog_product_view>
<observers>
<daanvandenbergh_redirectsimpleproducts>
<class>daanvandenbergh_redirectsimpleproducts/observer</class>
<method>redirectToConfigurable</method>
</daanvandenbergh_redirectsimpleproducts>
</observers>
</controller_action_predispatch_catalog_product_view>
</events>
</frontend>
</config>
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:

<?php
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 )
->getFirstItem();
$attributeOptions[ $productAttribute['attribute_id'] ] = $simpleProduct->getData( $productAttribute['attribute_code'] );
}
Mage::getSingleton( 'core/session' )->setSuperAttributes( $attributeOptions );
Mage::app()->getResponse()->setRedirect( $mainProduct->getProductUrl(), 301 );
Mage::app()->getResponse()->sendResponse();
exit;
}
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!

Buy me a beer?

Do you appreciate my work and support? Please consider supporting me by donating, so I can continue to develop and write useful solutions for you.

Choose amount

Personal Message

Thank you! 🙂

Written by Daan van den Bergh

Magento 2 Back-end Developer with a passion for trainlifting, airplane-gliding, hunting trees and creating fake hobbies.

4 Comments

Leave a Reply
  1. Hi Thank you for your post. I would like to ask you how could I rewrite this module to make it work for Magento 2? Any advice would be appreciated, cheers.

  2. Very interesting – as with the above poster, this is exactly what I’m looking for in Magento 2.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.