How To

Magento 2: How to Get Parent Product ID from Child Product ID for Bundled and Grouped Products

Hello Magento Friends,

Today’s blog is about fetching parent product ID from child product ID for bundled and grouped products in Magento 2.

Magento supports 6 Product Types to manage your catalog. In which Grouped Products and Bundled Products contain child products. When you have the requirement to get the parent product ID from the child product ID, here is the solution.

Let’s see  How to Get Parent Product ID from Child Product ID for Bundled and Grouped Products in Magento 2.

Steps to Get Parent Product ID from Child Product ID for Bundled and Grouped Products in Magento 2:

Step 1: Create a file at the below path

app\code\Vendor\Extension\Helper\Data.php

And add the below code

<?php
namespace Vendor\Extension\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{ 
    protected $bundletype;
    protected $grouptype;
    protected $_productFactory;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Bundle\Model\Product\Type $bundletype,
        \Magento\GroupedProduct\Model\Product\Type\Grouped $grouptype,
        \Magento\Catalog\Model\ProductFactory $_productFactory)
    {
        $this->bundletype=$bundletype;
        $this->grouptype=$grouptype;
        $this->_productFactory = $_productFactory;
        parent::__construct($context);
    }
 
    public function getBundleParentProduct($id)
    {
        $bundleproduct = $this->bundletype->getParentIdsByChild($id);
   
        if(isset($bundleproduct[0]))
        {
            $product= $this->_productFactory->create()->load($bundleproduct[0]);
            return $product;
         } 
         else
         {
             return false;
         }
    }

    public function getGroupParentProduct($id)
    {
        $groupproduct = $this->grouptype->getParentIdsByChild($id);

        if(isset($groupproduct[0]))
        {
            $product= $this->_productFactory->create()->load($groupproduct[0]);
            return $product;
        }
        else
        {
            return false;
        }
    }    
}

Step 2: Call function from the template file

<?php

$helper = $this->helper('Vendor\Extension\Helper\Data');
$ childproductid=33;
$parent_bundle = $helper->getBundleParentProduct($childproductid);
echo “Product Id : ”.$parent_bundle ->getId(); //get id of bundle parent product
echo “Product Name : ”.$parent_bundle ->getName(); //get name of bundle parent product

$parent_group = $helper->getGroupParentProduct($childproductid);
echo “Product Id : ”.$parent_ group ->getId(); //get id of group parent product
echo “Product Name : ”.$parent_ group ->getName(); //get name of group parent product
?>

Conclusion:

This way you can obtain Parent Product ID using Child Product ID for Bundled and Grouped Products with the help of the above solution. In case you are unable to get the output, connect with me through comments.

Happy Coding!

Click to rate this post!
[Total: 4 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago