Get parent products: Bundle, Grouped products
Magento supports two types of parent products: bundle products and grouped products.
In this guide, I'll walk you through a straightforward method to retrieve the IDs of parent products—both bundle and grouped—using Magento 2. These product types can be created from the Magento 2 backend and consist of multiple child products. If you're wondering whether it's possible to get the parent product IDs when you have the IDs of the child products, you're in the right place. Below, you'll find code snippets for each product type.
Bundle product
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 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 ); }
The two functions you should focus on in the class mentioned above are getChildrenIds
and
getParentIdsByChild
. To work with these functions, you need to define the product type as a
prerequisite. This means you can load the product and declare the function getTypeInstance
as shown
below:
$product->getTypeInstance()->getParentIdsByChild($child->getId());
In Magento, it's essential to understand the various product types available, such as configurable, bundle, and grouped products. Each type has its own structure and methods for handling relationships between parent and child products.
To access product information, you must first load the product object. This can be done using the product repository or factory methods, allowing you to retrieve the product's type instance and interact with its specific functions.
Once the product is loaded, you can use the getChildrenIds
and getParentIdsByChild
functions to manage product relationships effectively. This enables you to gather the necessary IDs for both parent
and child products, streamlining your operations within the Magento store.