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 184.108.40.206 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!