In Magento 1 I had an extension that did the following to get the configurable parent product by child ID:
// $full_product holds the child in the first place
$configurable_product_model = Mage::getModel('catalog/product_type_configurable');
$parentIds= $configurable_product_model->getParentIdsByChild($full_product->getId());
if (count($parentIds) > 0) {
$full_product = Mage::getModel('catalog/product')->load($parentIds[0]);
}
How can I do the same in Magento2?
Best Answer
In the magento 2 , you need to follow the below steps to get the class methods and to inject its object to our class rather that pulling the object this is what we called the Dependency Injection.
Step 1:
Define the class that your are going to get the object with the keyword
Step 2:
you need to initialize the configurable product class in your class
Step 3:
If you need to get the associated simple products from the configurable you can try the below code