I have created a custom column in Product gird by a module, which shows Parent product's SKU there. It's working fine but when I try to filter it Magento gives "Something went wrong with processing the default view and we have restored the filter to its original state." error. and in exception.log the error is:
[2019-01-03 11:33:08] main.CRITICAL: Invalid attribute name: parent_sku {"exception":"[object] (Magento\Framework\Exception\LocalizedException(code: 0): Invalid attribute name: parent_sku at /html/vendor/magento/module-eav/Model/Entity/Collection/AbstractCollection.php:1383)"} []
Here's my code
/html/app/code/CustomColumn/Addproductcolumn/view/adminhtml/ui_component
<?xml version="1.0" encoding="utf-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="product_columns">
<column name="parent_sku" class="CustomColumn\Addproductcolumn\Ui\Component\Listing\Column\ParentProductId">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Parent Sku</item>
<item name="filter" xsi:type="string">text</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="sortOrder" xsi:type="number">50</item>
<item name="align" xsi:type="string">left</item>
<item name="dataType" xsi:type="string">text</item>
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
</item>
</argument>
</column>
</columns>
</listing>
app/code/CustomColumn/Addproductcolumn/Ui/Component/Listing/Column/ParentProductId.php
<?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;
protected $bundle;
protected $_productFactory;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable,
\Magento\Bundle\Model\Product\Type $bundle,
array $components = [],
array $data = []
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$this->_configurable = $configurable;
$this->_bundle = $bundle;
$this->_productFactory = $productFactory;
}
public function prepareDataSource(array $dataSource)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as &$items) {
if($items['type_id'] == 'simple'){
$productId = $items['entity_id'];
$parentProducts = '';
$getParent = $this->_configurable->getParentIdsByChild($productId);
$parentData = $this->_productFactory->create()->load($getParent);
$getParentSku = $parentData->getSku();
if(isset($getParentSku)){
$parentProducts .= $getParentSku;
}
$getParent = $this->_bundle->getParentIdsByChild($productId);
if($getParent != ''){
foreach ($getParent as $p) {
$parentData = $this->_productFactory->create()->load($p);
$getParentSku = $parentData->getSku();
$parentProducts .= $getParentSku.',';
}
}
$items['parent_sku'] = $parentProducts;
}
}
}
return $dataSource;
}
}