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 | 12-18-2024

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

Magento supports two types of the parent products, including bundle products and grouped products.

Today I will lead you in the simple way to get parent products: bundle and grouped products in Magento 2. The bundle and grouped products are two of the product types you can create from the backend of the 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());

Final words

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.

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    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