0

I have created a UI listing in Magento 2. The first column was getting the id of the product. Since I don't want to display the id, I wanted to display its SKU. So I applied a class for the column.

     <column name="product_id" class="Avesh\Rule\Ui\Component\Listing\Column\Rules\Sku">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="sorting" xsi:type="string">asc</item>
                <item name="label" xsi:type="string" translate="true">SKU</item>
            </item>
        </argument>
    </column>

Now I added a filter to filter the column by Sku. It doesn't work. But if I put the id of the product it works fine. What I am missing?

Searching with SKU enter image description here

Searching with Id enter image description here

Undefined index: sku in /var/www/vhosts/website/app/code/Avesh\Rule/Ui/Component/Listing/Column/Rules/Sku.php on line 37 I don't have a colum name in my table as sku. just have a product Id when i change it to

    <column name="sku" class="Avesh\Rule\Ui\Component\Listing\Column\Rules\Sku">

Class code

namespace Avesh\Rule\Ui\Component\Listing\Column\Rules;

use Magento\Ui\Component\Listing\Columns\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Catalog\Model\ProductFactory;

class Sku  extends Column
{
protected $productFactory;

public function __construct(
    ContextInterface $context,
    UiComponentFactory $uiComponentFactory,
    array $components = [],
    array $data = [],
    ProductFactory $productFactory
) {
    parent::__construct($context, $uiComponentFactory, $components, $data);
    $this->productFactory = $productFactory;
}

public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        $fieldName = $this->getData('name');
        foreach ($dataSource['data']['items'] as & $item) {
            if ($item[$fieldName] != 0) {
                $adminName = $this->getProductSku($item[$fieldName]);
                $item[$fieldName] = $adminName;
            }
        }
    }
    return $dataSource;
}

private function getProductSku($productId) {
    $product = $this->productFactory->create()->load($productId);
    return $product->getSku();
}
}
11
  • Can you please add screenshot what you are facing error? Commented Jan 28, 2019 at 8:45
  • Added please check.
    – Avesh Naik
    Commented Jan 28, 2019 at 8:53
  • 1
    For that you need to add custom filter Please study product grid.. where store view is not column still we can make filter on store view.. how they added , just go to step by step you will understand hoe to add custom filter Commented Jan 28, 2019 at 10:31
  • 1
    have you tried changing some values on your getProductsSku function?
    – fmsthird
    Commented Jan 28, 2019 at 10:47
  • I think the problem was there.
    – fmsthird
    Commented Jan 28, 2019 at 10:47

1 Answer 1

3

Add this in your collection file.

protected function _renderFiltersBefore() {

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $request = $objectManager->get('Magento\Framework\App\Request\Http');
    $filters = $request->getParam("filters");
    if (isset($filters['product_id'])) {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->get('Magento\Catalog\Model\ProductFactory')->create()
            ->getCollection()
            ->addAttributeToFilter('sku', ['eq' => $filters['product_id']])->getFirstItem();
        $this->getSelect()->orWhere("product_id = '" . $product->getId() . "'");
    }

    parent::_renderFiltersBefore();
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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