How to Get Parent Product ID in Magento 2
The post shows the programmatic method to get parent product ID in Magento 2. Magento 2 has 3 product types named configurable, bundled and grouped, those have children products. You might be wondering if you can get parent product ID of these 3 product types.
You can use the solution if you are selling the store products on 3rd party platforms like Facebook shop and care to enhance the shopping journey.
For example, I used the below method for a client who wanted to redirect the customers to the website directly on the cart page from Facebook shop product. And the product had to be already added to the cart. The customer can skip the step to manually add the product to the cart in the Magento 2 store. However, to implement it, I needed to get the parent product ID as the simple product configuration can’t be displayed.
Hence, the solution:
Method to Get Parent Product ID in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
namespace [Vendor]\[Module]\Helper; use Magento\Framework\App\Helper\Context; class Data extends AbstractHelper { protected $configurable; protected $grouped; public function __construct( Context $context, \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable, Magento\GroupedProduct\Model\Product\Type\Grouped $grouped ) { $this->configurable = $configurable; $this->grouped = $grouped; parent::__construct($context); } public getParentId($childId){ /* for simple product of configurable product */ product = $this->configurable->getParentIdsByChild($childId); if(isset($product[0])){ return $product[0]; } /* for simple product of Group product */ $parentIds = $this->grouped->getParentIdsByChild($childId); /* or for Group/Bundle Product */ $product->getTypeInstance()->getParentIdsByChild($childId); } } |
Mention your doubts in the Comments section below and I’d be happy to solve them for you.
Feel free to share the post with fellow developers on social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
4 Comments
Getting this error with it: Parse error: syntax error, unexpected ‘getParentId’ (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /chroot/home/a2bf3fe2/newsite.com/app/code/Nplug/Gprod/Helper/context.php on line 20
Hello James,
Which Magento version you are using?
Thank You
It seems to don’t work. I’m trying with a bundled product and I’m getting an empty array as described here: https://github.com/magento/magento2/issues/6618
Was this already fixed?
Hello Magno,
Please try this:
Thank You