Hyvä Theme is Now Open Source: What This Means for Magento Community - Mageplaza
Hyvä is now Open Source and free. Discover what changed, what remains commercial, how it impacts the Magento ecosystem, and how to maximize its full potential.
Vinh Jacker | 03-17-2025
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!
Before diving into the methods, let’s briefly understand how bundled and grouped products function in Magento:
Getting the parent product for a child product can be useful in many scenarios, such as:
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);
}
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());
To ensure efficient and scalable parent-child product management, follow these tips:
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.
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.