cancel
Showing results for 
Search instead for 
Did you mean: 

Custom column in Product Listing Grid not being filtered?

Custom column in Product Listing Grid not being filtered?

Hi, 

 

I have created a custom column in Product Listing 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: parentid_col {"exception":"[object] (Magento\Framework\Exception\LocalizedException(code: 0): Invalid attribute name: parentid_col at /html/vendor/magento/module-eav/Model/Entity/Collection/AbstractCollection.php:1383)"} []

 

here's my module's code

 

/app/code/CustomColumn/Addproductcolumn/view/adminhtml/ui_component/product_listing.xml

<?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="parentid_col" 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 id</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)
    {


        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(isset($getParent)){

                    foreach ($getParent as $p) {
                        $parentData = $this->_productFactory->create()->load($p);
                        $getParentSku = $parentData->getSku();
                        $parentProducts .= $getParentSku.',';
                      }  
                        $items['parentid_col'] = $parentProducts;
                }

            }
        }
    }
        return $dataSource;
    }


}

Kindly, guide me, what I am doing wrong here or what I'm missing here?