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,
<?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,
$childId = 10;
echo $block->getParentProductId($childId);