Magento – How to Redirect Associated Product to Configurable Product

associate-productscatalogconfigurable-productredirect

I am using Magento 1.9.2.1 and I am using the native swatches option. I am trying to only show the associated products on the catalog page and not the configurable product. But I am hoping that by clicking on any of the associated products, it would take the customer to the configurable product.

So lets say that I am selling a cap that has a red and brown option.
I want the catalog to show the red simple product and the brown simple product. But when the customer clicks on either of the simple products, it will take them to the configurable product and they can select a color there instead.

Is there a easy way to do this?

Best Answer

You need to implement some method like this (e.g. in data helper of your module):

/**
 * @param array $simpleIds
 *
 * @return array
 */
public function getConfigurableItems($simpleIds)
{
    $collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
        ->setFlag('require_stock_items', true)
        ->setFlag('product_children', true);

    $collection->getSelect()->where('link_table.child_id IN (?)', $simpleIds)->columns(array('link_table.child_id'));
    $result = array();
    foreach ($collection as $item) {
        $result[$item->getChildId()] = $item;
    }

    return $result;
}

The output of it will be an array where key is ID of simple product and value is object of configurable product.

Related Topic