Getting Configurable Product from Simple Product ID in Magento 1.5+

Advertisements

I recently stumbled across a hurdle which stopped some of my code from working. It was code that gets the configurable product associated with a given simple product. Many solutions out there call a “loadParentProductIds()” function within the Mage_Catalog_Model_Product class. However, as of Magento 1.4.2.0, they deprecated this method, simply by setting the data element (and returning) an empty array. So any calls to this function would return/yield no parents. Hm, how to get the parent product now?

Finally, I managed to find a workable solution. Check this out;

$simpleProductId = 465;
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')
                  ->getParentIdsByChild($simpleProductId);
$product = Mage::getModel('catalog/product')->load($parentIds[0]);
echo $product->getId(); // ID = 462 (aka, Parent of 465)

There you have it. Short and sweet.

Join the Conversation

6 Comments

  1. This isn’t a change to Magento’s core. It’s a solution for when you’re writing your own custom modules. It would go in place of wherever you called the previous Mage_Catalog_Model_Product::loadParentProductIds() method, to use this code instead to get the parent product ID of a simple product that you currently have loaded.

  2. Tomas,
    Wherever you need to. This is just an instructional post, for anyone writing their own extensions and need to find out/retrieve the parent configurable product model given a simple product model/id.

Leave a comment

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