Please provide suggestions
$product->getTypeInstance()->getParentIdsByChild($child->getId());
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.
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; }