I'm trying to get parent products id for child products but i'm getting empty result.
<?php
namespace CustomColumn\Addproductcolumn\Ui\Component\Listing\Column;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Listing\Columns\Column;
class ParentProductId extends Column
{
protected $configurable;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
\Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurable,
array $components = [],
array $data = []
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$this->_configurable = $configurable;
}
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as &$items) {
if($items['type_id'] == 'simple'){
$productId = $items['entity_id'];
$getParent = $this->_configurable->getParentIdsByChild($productId);
if($getParent){
$items['parentid_col'] = $getParent[0];
}else{
$items['parentid_col'] = "nothing found";
}
}
}
}
return $dataSource;
}
}
?>