Magento 2 Get Configurable product id from Child id of configurable. When you have Child id of the P product is available you can simply get the parent config product id.
Create simple block to get parent product id,
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 | <?php namespace Rbj\Product\Block; class Product extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable, array $data = [] ) { $this->configurable = $configurable; parent::__construct($context, $data); } /** * @param int $childId * @return int */ public function getParentProductId($childProductId) { $parentConfigObject = $this->configurable->getParentIdsByChild($childProductId); if($parentConfigObject) { return $parentConfigObject[0]; } return false; } } |
now call the function in template file to get Parent Id,
1 2 | $childId = 10; echo $block->getParentProductId($childId); |