Get parent products: Bundle, Grouped products in Magento 2

Today I will lead you in the simple way to get parent products: bundle and grouped product in Magento 2.

Today I will lead you in the simple way to get parent products: bundle and grouped product in Magento 2. The bundle and grouped products are two of the product types you can create from the backend of Magento 2 store and they include some children. You are wondering whether it is possible to get parent product’s ids or not if you are holding the children product’s ids. Here is the right place to guide you what you should do. Lets’s come with the code snippet of 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
   );
}

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());

The topic is all required implementations you can get parent products: Bundle and Grouped products. I am so happy when I help you shorten much time but still get more efficient work.

Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
get
parent
products
bundle
products
grouped
products

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and a wanna-be influencer. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 get parent products bundle products grouped products
  • magento2 get parent products
  • get parent product id in magento 2
  • get parent category id product magento 2
  • magento 2 get parent product id
  • magento 2 load product by id
  • magento load product by id
  • magento 2 get parent product
  • magento 2 get bundle product items
  • magento 2 get parent product from simple
  • magento 2 get child products of grouped
  • magento 2 get grouped product associated products
  • magento 2 get associated products of grouped product
  • magento 2 get child products of bundle
  • magento get parent product
  • magento 2 check if product has parent
  • magento 2 check if simple product has parent
  • magento 2 get parent category
  • magento 2 get child products of configurable
  • magento 2 get parent products
  • magento 2 get all simple products of configurable
  • magento 2 check if product is child of configurable
  • 2.2.x, 2.3.x