Anyone have an idea how we can achieve this.
2 Answers
Your requirement looks quite interesting. Well, if you want to do it by your own then you will have to check and customise the magento core (UI Component) code for product's admin grid. You will have to change particular filter query for sku column.
I have also found one ready made (paid) extension on magento marketplace which completely suits and satisfy your requirement. You may check it at https://marketplace.magento.com/easygento-magento2-productgridskufilter.html
Thanks,
Step 1) Create di.xml
<preference for="Magento\CatalogSearch\Model\ResourceModel\Search\Collection" type="Vendor\Module\Model\Search" />
<preference for="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider" type="Vendor\Module\Model\ProductDataProvider" />
Step 2) Create Search.php file
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Model;
class Search extends \Magento\CatalogSearch\Model\ResourceModel\Search\Collection
{
/**
* Add backend search query filter (search by all stores)
*
* @param string $query
* @return $this
*/
public function addBackendSearchFilter($query)
{
$this->_searchQuery = $query;
$valuePart = explode(",", $query);
if(count($valuePart) > 1)
{
foreach ($valuePart as $value) {
$retStr[]= trim($value);
}
$this->addFieldToFilter(
'sku',
['in' => $retStr]
);
}
else
{
$this->addFieldToFilter(
$this->getEntity()->getLinkField(),
['in' => new \Zend_Db_Expr($this->_getSearchEntityIdsSql($query, false))]
);
}
return $this;
}
}
Create ProductDataProvider.php
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Vendor\Module\Model; class ProductDataProvider extends \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider { /** * @inheritdoc */ public function addFilter(\Magento\Framework\Api\Filter $filter) { if (isset($this->addFilterStrategies[$filter->getField()])) { $this->addFilterStrategies[$filter->getField()] ->addFilter( $this->getCollection(), $filter->getField(), [$filter->getConditionType() => $filter->getValue()] ); } elseif ($filter->getField() == "sku" && count(explode(",",str_replace("%","",$filter->getValue()))) > 1) { $withComma = explode(",",str_replace("%","",$filter->getValue())); $attrs = array(); foreach ($withComma as $cItem) { $attrs[] = ['attribute' => $filter->getField(), $filter->getConditionType() => '%'.trim($cItem).'%']; } $this->getCollection()->addAttributeToFilter($attrs); } else { parent::addFilter($filter); } } }
-
Hello, I tried your solution and it's working only if I put directly the override in vendor files, did you manage to make this solution works by using override in a custom module ? Thank you ! Commented Dec 10, 2020 at 15:01