Magento 2 – How to Get Parent Product ID

configurable-productmagento2productproduct-attribute

How to get a child's parent product id in Magento 2 for the configurable products?

I want to get the parent product id of child product in Magento based on child product id.

Best Answer

For getting the parent product id in your phtml file, you can call code directly by:

    $productId = 52; //this is child product id
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($productId);
     if(isset($product[0])){
         //this is parent product id..
         echo $product[0];
    }
Related Topic