Difficulty: Medium
Modification: Core Controller
Q:
I am trying to find a way to redirect the url for a simple product to go to the corresponding configurable product. For example if I have a link to a small blue t-shirt, it would go to the main t-shirt page where the attributes are available to select.
A:
You will need to modify the ProductController to make it look for a configurable product and display it when someone asks for a simple product.
The file you need to modify is /app/code/core/Mage/Catalog/controllers/ProductController and the method name is _initProduct().
Find the code which retrieves the product to be displayed:
$product = Mage::getModel(‘catalog/product’)
->setStoreId(Mage::app()->getStore()->getId())
->load($productId); And add this code after it:
if($product->type_id==“simple”)
{
$parentId=$product->loadParentProductIds()->getData('parent_product_ids');
if(isset($parentId[0]))
{
$product=Mage::getModel('catalog/product')->load($parentId[0]);
}
} That’s it, simple and elegant.



Hi,
I used this code and it works great!
However, if i want to link to the configurable product while the attribute is already selected?
For example, I clicked on the BLUE color t-shirt, I need that the link will show the BLUE color chosen (along with it’s image, as I set up the product page according to this wiki
Important note: the
$product->loadParentProductIds()has been deprecated as of Magento CE 1.4.2.0! I wrote a note on how to do it in the latest versions of Magento here: http://gabrielsomoza.com/magento/getting-a-products-parent-id-in-magento/ – hope it helps!