Magento 1.8 – Configurable Product with Different URLs for Simple Products

configurable-productmagento-1.8product-urlsurl

If I sell a t-shirt which has 2 colours, normally I would have 1 URL for the configurable product as the 2 simple products would be set to have a visibility of hidden.

However I would quite like to replicate the functionality offered on this site, http://goo.gl/55VLRt, where each time you choose a variant then the URL changes. Is this possible with Magento?

Best Answer

I don't have much code for this, but I have an idea.
You will have to do things a bit different than the default approach Magento has for configurable products.
By default, the configurable product is visible and the simple products associated are not visible.
You will have to do it the other way around. Configurable product is not visible and the simple products are visible.
And in the simple product page check if it's part of a configurable product.
You can get the configurable product ids that contain the current simple product like this:

$productId = $this->getProduct()->getId();
$parentIds = Mage::getSingleton('catalog/product_type_configurable')->getParentIdsByChild($productId);

This will return an array because a simple product can be part of more simple products. This usually does not happen, but it's possible,
Of you are sure you have only one configurable product for the simple one then you can use $parentIds[0].
If $parentIds[0] is not set, then the simple product is not part of any configurable products.
Now you have the configurable product id.

You will need all the simple products associated to the configurable product:

$parentId =  $parentIds[0];
$configurableProduct = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($parentId);
$simpleProducts = $configurableProduct->getTypeInstance()->getUsedProducts();

Now you have all the simple products from the configurable products.
You need to display in your product page a dropdown with all the simple products that will change the url when you select a value.

something like this: Let's say that color is your configurable attribute.

<select onchange="setLocation($(this).value)">
    <?php foreach ($simpleProducts as $simpleProduct) : ?>
        <?php 
             $selected = '';
             if ($simpleProduct->getId() == $this->getProduct()->getId()) {
                 $selected = ' selected="selected"';
             }
        ?>
        <option<?php echo $selected;?> value="<?php echo $simplProduct->getProductUrl()?>"><?php echo $simpleProduct->getAttributeText('color')?></option>
    <?php endforeach;?>
</select>

I haven't tested the code, but seams like the way to go.