cancel
Showing results for 
Search instead for 
Did you mean: 

How to check product has parent id using product object ($product = $item->getProduct();)

How to check product has parent id using product object ($product = $item->getProduct();)

Please provide suggestions

3 REPLIES 3

Re: How to check product has parent id using product object ($product = $item->getProduct();)

 $product->getTypeInstance()->getParentIdsByChild($child->getId());

 


 


 

Re: How to check product has parent id using product object ($product = $item->getProduct();)

Hello @anand_v

 

Try this

 

$productId = 10; //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]; }

 

Note: $objectManager is used here for demonstration purpose only.

If you find my answer useful, Please click Kudos & Accept as Solution.

Re: How to check product has parent id using product object ($product = $item->getProduct();)

Hi,

 

 

 protected $_catalogProductTypeConfigurable;

 public function __construct(            \Magento\Catalog\Block\Product\Context $context,       
            //for getting parent id of simple            \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $catalogProductTypeConfigurable,            array $data = []
        ) {
               //for getting parent id of simple            $this->_catalogProductTypeConfigurable = $catalogProductTypeConfigurable;            parent::__construct($context, $data);
        }
    public function getParentProductData($id){ 
            $parentByChild = $this->_catalogProductTypeConfigurable->getParentIdsByChild($id);
            if(isset($parentByChild[0])){
                //set id as parent product id...                $id = $parentByChild[0];          
            }
            return $id;     
        } 

 

Prema M