2

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: 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 code

/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="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;
    }


}
  • How did you add this attribute? – Anshu Mishra Jan 7 at 5:18
  • @AnshuMishra i have created module to add this attribute in product listing grid – Tajveez Rehman Jan 7 at 12:02
  • Please share the code that you have written to add the custom attribute. – Anshu Mishra Jan 7 at 12:03
  • @AnshuMishraI I've already did that above in question's description – Tajveez Rehman Jan 7 at 13:05
0

You need to add this below code in your ParentProductId.php file :

protected function _initSelect()
{
    parent::_initSelect();
    $this->getSelect()->joinLeft
    (
        ['custom_table_name' => $this->getTable('custom_table_name')],
        'main_table.entity_id = custom_table_name.customer_id',
        ['filed_you_want_to_display']
    );
    return $this;
}
  • Thanks, Rohan but I don't have a custom table for this custom attribute. what should I do in this scenario? – Tajveez Rehman Jan 7 at 6:28
  • i have added this but its still giving the same error – Tajveez Rehman Jan 7 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.