Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Get Parent Products for Bundle & Grouped Products in Magento 2

Vinh Jacker | 03-17-2025

How to Get Parent Products for Bundle & Grouped Products in Magento 2

Magento, as a powerful eCommerce platform, offers various product types, including simple, configurable, bundle, and grouped products. If you’re dealing with bundle and grouped products, you may need to retrieve the parent product for a specific child product. This can be useful for customizing product displays, managing stock, or improving customer experience.

In this guide, we’ll explore how to get parent products for bundle and grouped products in Magento. Let’s get started!

Understanding Bundle & Grouped Products in Magento

Before diving into the methods, let’s briefly understand how bundled and grouped products function in Magento:

  • Bundle Products: These allow customers to create customized product sets by selecting different components. Each component is a separate simple product.
  • Grouped Products: A collection of related products displayed as a single unit, where customers can choose multiple variations at once. Unlike bundle products, grouped products do not have dependencies between child and parent items.

Why Retrieve Parent Products in Magento?

Getting the parent product for a child product can be useful in many scenarios, such as:

  • Displaying related parent products on product detail pages.
  • Managing stock based on parent-child relationships.
  • Enhancing SEO and structured data by associating products properly.
  • Customizing pricing rules for bundle or grouped products.

Get Parent Product for Bundle product

Bundle Products contain multiple Simple Products, but unlike Configurable or Grouped Products, the relationship is flexible.

Go to the class Magento\Bundle\Model\Product\Type, there are two functions:

/**
* Retrieve Required children ids
* Return grouped array, ex array(
*   group => array(ids)
* )
*
* @param int $parentId
* @param bool $required
* @return array
*/
public function getChildrenIds($parentId, $required = true)
{
   return $this->_bundleSelection->getChildrenIds($parentId, $required);
}
 
/**
* Retrieve parent ids array by required child
*
* @param int|array $childId
* @return array
*/
public function getParentIdsByChild($childId)
{
   return $this->_bundleSelection->getParentIdsByChild($childId);
}
 
Configurable product: You can see the class Magento\ConfigurableProduct\Model\Product\Type\Configurable, it also has two functions:
 
/**
* Retrieve Required children ids
* Return grouped array, ex array(
*   group => array(ids)
* )
*
* @param  array|int $parentId
* @param  bool $required
* @return array
*/
public function getChildrenIds($parentId, $required = true)
{
   return $this->_catalogProductTypeConfigurable->getChildrenIds($parentId, $required);
}
 
/**
* Retrieve parent ids array by required child
*
* @param  int|array $childId
* @return array
*/
public function getParentIdsByChild($childId)
{
   return $this->_catalogProductTypeConfigurable->getParentIdsByChild($childId);
}

Get Parent Product for Grouped product

Grouped Products contain multiple Simple Products grouped together. To get the parent of a Simple Product in a Grouped Product, go to the class Magento\GroupedProduct\Model\Product\Type\Grouped:

/**
* Retrieve Required children ids
* Return grouped array, ex array(
*   group => array(ids)
* )
*
* @param int $parentId
* @param bool $required
* @return array
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function getChildrenIds($parentId, $required = true)
{
   return $this->productLinks->getChildrenIds(
       $parentId,
       \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED
   );
}
 
/**
* Retrieve parent ids array by requested child
*
* @param int|array $childId
* @return array
*/
public function getParentIdsByChild($childId)
{
   return $this->productLinks->getParentIdsByChild(
       $childId,
       \Magento\GroupedProduct\Model\ResourceModel\Product\Link::LINK_TYPE_GROUPED
   );
}

Two functions you want to refer in the above class are getChildrenIds and getParentIdsByChild. By this way, defining the product type is the prerequisites, so for sure, you can load product and declare the function getTypeInstance as the following:

$product->getTypeInstance()->getParentIdsByChild($child->getId());

Best Practices for Managing Parent-Child Relationships

To ensure efficient and scalable parent-child product management, follow these tips:

  • Use Magento’s Built-in Repositories – They are optimized and follow Magento’s best practices.
  • Avoid Hardcoded Queries – Direct SQL queries can lead to database inconsistency and upgrade issues.
  • Cache Parent Product Data – Reduce API calls and database queries by caching frequently accessed data.
  • Leverage GraphQL (For Headless Magento) – If using GraphQL, implement a custom query to fetch parent products dynamically.

FAQs

1. Why do I need to get the parent product of a child product in Magento?

Fetching the parent product is useful for inventory management, SEO, product displays, pricing rules, and structured data. It helps ensure the correct parent-child relationship is maintained for bundle, configurable, and grouped products.

2. Can I retrieve the parent product using a SQL query?

Yes, you can use direct SQL queries to find parent products:

For Configurable Products:

SELECT parent_id FROM catalog_product_relation WHERE child_id = (SELECT entity_id FROM catalog_product_entity WHERE sku = 'child-sku');

For Bundle Products:

SELECT parent_product_id FROM catalog_product_bundle_selection WHERE product_id = (SELECT entity_id FROM catalog_product_entity WHERE sku = 'child-sku');

For Grouped Products:

SELECT parent_id FROM catalog_product_relation

3. How do I get the parent product in Magento using GraphQL?

Magento’s GraphQL API allows dynamic retrieval of parent-child relationships. You can use the products query to fetch associated products:

query {
  products(filter: { sku: { eq: "child-sku" } }) {
    items {
      id
      name
      configurable_product_options_selection {
        parent_sku
      }
    }
  }
}

This returns the parent SKU for a given child product.

4. How can I display the parent product on a child product page?

Use Magento’s layout XML or custom block to fetch the parent product and display it on the child product page.

Example in phtml template:

$parentIds = $this->getConfigurableParentIds($childProduct->getId());
if (!empty($parentIds)) {
    $parentProduct = $productRepository->getById($parentIds[0]);
    echo $parentProduct->getName();
}

5. Can I get multiple parent products for a single-child product?

Yes, a child product can belong to multiple configurable, grouped, or bundle products. When retrieving parent IDs, always check for multiple values in the returned array.

Final words

Retrieving parent products for bundle and grouped products in Magento is essential for better inventory management, improved product displays, and enhanced SEO. By following the methods outlined above, you can efficiently fetch parent products and optimize your Magento store for a seamless shopping experience.

x
    Jacker

    Jacker is the Chief Technology Officer (CTO) at Mageplaza, bringing over 10 years of experience in Magento, Shopify, and other eCommerce platforms. With deep technical expertise, he has led numerous successful projects, optimizing and scaling online stores for global brands. Beyond his work in eCommerce development, he is passionate about running and swimming.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services