0

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;
}

}
  • Did you used "parentid_col" anywhere in your code? – Prachi Saxena Jan 31 at 6:46
  • @PrachiSaxena "parentid_col" was the column name which i later changed to "parent_sku", sorry I have posted old exception when column name was "parentid_col" – Tajveez Rehman Jan 31 at 6:49
  • Have you created any product attribute "parent_sku" or column to save data in database using install/upgrade script? – Prachi Saxena Jan 31 at 7:06
  • No, i have not created anything on the database, i have just created a custom column in product listing with product_listing.xml and ProductParentId.php is used to get parent ids of each product and display in the custom column. I have tried creating a product attribute with "parent_sku" although it doesn't give any error while filtering but whatever you search the grid comes empty. – Tajveez Rehman Jan 31 at 7:13
  • Did you saved any value in that attribute which you created using any observer or plugin? – Prachi Saxena Jan 31 at 7:23
1

You will need to create a custom product attribute "parent_sku". You can refer this link for creating one: here

You will then need to save that attribute value. You can do that using an observer for the event "catalog_product_save_after". In your observer:

<?php
namespace YOURNAMESPACE\YOURMODULENAME\Observer;
use Magento\Framework\Event\ObserverInterface;
class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        // Add your lofic to get $parentProducts and save the product
    }   
}

Then in ui_component just specify the column and remove your custom class ParentProductId from it. Like this:

<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">
<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>

Check in the product grid after upgrade.

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.