Getting a Product’s Parent ID in Magento
I recently faced the challenge of listing all parent IDs for a given Magento product. The solution – while definitely not overly complicated – wasn’t as straightforward as I first thought, and documentation in the matter is surprisingly scarce. So here’s how to get the parent IDs for a given product.
Magento < 1.4.2
In early versions of Magento (prior to 1.4.2) getting a Product’s parents was as simple as:
list( $parentId ) = $product->loadParentProductIds()
->getData('parent_product_ids');
Magento >= 1.4.2
As of Magento 1.4.2.0 that function is deprecated and the correct way of doing it is:
list( $parentId ) = Mage::getModel('catalog/product_type_configurable')
->getParentIdsByChild( $product->getId() );
Note we’re using Product Type models. There’s different kinds of “Composite” product types in Magento, including the Grouped and Configurable types. This means that if the parent of $product is a Grouped product instead of a Configurable product, then we need to get its model using the 'catalog/product_type_grouped' model class instead.
If this was useful to you please leave a comment, link it in the forums or share it right below these lines!
How do you know if the parent product is a grouped product instead of a configurable?
in otherwords…how do I know to use the product_type_configurable vs the product_type_grouped
Hi Jamie,
As far as I know there’s no way to know what type the parent is until you find the parent. I wish I knew a generic way to find the parent of a product without resorting to its type at all, but I looked around and I couldn’t find a good solution.
The reason why this is hard is because configurable and grouped relationships are stored in different tables. My suggestion is that if you really need to get the parent without knowing which type it is before-hand, just make one query for each type and then merge the results. You should end up with only one result anyways in most cases (if not all). Let me know if you need an example on how to do that.
$productType = $_product->getTypeId();
if ($productType == ‘grouped’) {
$parentIds = Mage::getModel(‘catalog/product_type_grouped’)->getParentIdsByChild($_product->getId());
if ($parentIds) {
$productId = $parentIds[0];
}
} else if ($productType == ‘configurable’) {
$parentIds = Mage::getModel(‘catalog/product_type_configurable’)->getParentIdsByChild($_product->getId());
if ($parentIds) {
$productId = $parentIds[0];
}
}
Hi Dipankar,
Thanks for your suggestion. But if $_product contains a simple product, the way I understand it, then getTypeId() will return ‘simple’ instead of ‘grouped’ or ‘configurable’ (correct me if I’m wrong) – so that code would only work if $_product is already the parent product.
Hi Dipankar,
Nice approach, but I have a code using something similar to the second example, and we have a problem:
If the product is a single product but this is used in more than one configurable product (has 2 fathers), we will get an array of 2 itens on getParentIdsByChild, and cant load our product correctly, because we could be using the incorrect parent product.
In my example its used at checkout/cart/item/default.phtml, so I cant determine this way witch one is the correct parent.
Thank you for the post anyway.