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

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

22 hours ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

22 hours ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago