Getting a Product’s Parent ID in Magento

Posted by Gabriel Somoza & filed under 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!

13 thoughts on “Getting a Product’s Parent ID in Magento”

  1. Jamie Kahgee

    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

    Reply
  2. Gabriel Somoza

    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.

    Reply
    • Enrique

      well the central part of this is to remember that a product has a parent only in the context of a quote item, so we should start from there (and that’s not a problem at all because if you are searching the parent of some product you probably have the quote item too). So you can loop $quote->getAllItems() compare with your $productId, and for configurable and bundled you can check $quoteItem->getParentItemId() and for grouped products you can check $quoteItem->getProductType()

      Reply
  3. Dipankar

    $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];
    }
    }

    Reply
    • gabrielsomoza

      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.

      Reply
  4. Ricardo Martins

    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.

    Reply
    • Jey Navam

      Hi,

      all my simple products are assigned to more than one configurable products, there for im having problem with find the correct parent product.

      Any Ideas or suggestion?

      Reply
      • gabrielsomoza

        Since your simple products are associated with more than one configurable product, the first step would be to define what “the correct parent product” means to you (or your client). Do you have any criteria to identify which of all the parent products is the “correct” one? Because without some additional criteria I’m afraid *ALL* of the parent products are the “correct” ones..

        Reply
        • Jey Navam

          Hi,

          Thanks for the reply,

          yes product ref_color attribute can be used as criteria to find the correct parent product by using simple product color attribute but for some reason I’m failing to find the correct parent. :(

          Reply
          • gabrielsomoza

            In that case the easy solution would be to first load all parent products and then pick the one that has the ref_color that matches your simple product.

            The best solution (but more complicated to implement) would be to combine the filter for ref_color together with the query that gets the parent products. The result would be one single query that both finds all parents and then returns only the one that has a matching ref_color.

  5. Naresh

    Thanks for the great tutorial….. How can i get current product’s Owner ID ?

    sry for cross posting

    Reply
    • gabrielsomoza

      What do you mean by “Owner ID”? If you mean “Parent ID” then this tutorial itself is just what you’re looking for (except that by now it may be slightly outdated).

      Reply

Leave a Reply