0

I am using Magento ver. 2.4.4. I have a custom grid, displaying the below fields data.

item_id, rma_id ,product_sku ,order_item_id ,reason_id resolution_id ,condition_id.

I want to create a filter for SKU, where we can put comma-separated or space-separated SKU & it will show results based on it.

Is there any way it can be done? Kindly let me know your thoughts on this.

3

1 Answer 1

1
+50

As i correct understand, you are using UI Component. Following example how to add multiply SKU filter on product grid. I hope you can properly add to your custom grid

Acme/StackExchange/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">
    <listingToolbar name="listing_top">
        <filters name="listing_filters">
            <filterInput name="sku" template="ui/grid/filters/field" class="Acme\StackExchange\Ui\Component\Listing\Filter\Sku" provider="${ $.parentName }">
                <settings>
                    <dataScope>sku</dataScope>
                    <label translate="true">SKU</label>
                </settings>
            </filterInput>
        </filters>
    </listingToolbar>
    <!-- Reset default filter. Just don't define filter in your column -->
    <columns name="product_columns">
        <column name="sku">
            <settings>
                <filter>false</filter>
            </settings>
        </column>
    </columns>
</listing>

Acme/StackExchange/Ui/Component/Listing/Filter/Sku.php

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Ui\Component\Listing\Filter;

use Magento\Ui\Component\Filters\Type\Input;

class Sku extends Input
{
    /**
     * @inheridoc
     */
    protected function applyFilter(): void
    {
        if (isset($this->filterData[$this->getName()])) {
            $value = explode(',', $this->filterData[$this->getName()]);
            $value = array_map('trim', $value);

            $filter = $this->filterBuilder->setConditionType('in')
                ->setField('sku')
                ->setValue($value)
                ->create();

            $this->getContext()->getDataProvider()->addFilter($filter);
        }
    }
}

[Update]

In example with use logic with reset filter your module should depends on original logic. In this example:

Acme/StackExchange/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Acme_StackExchange">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Example with the same logic, but with custom filter name

Acme/StackExchange/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">
    <listingToolbar name="listing_top">
        <filters name="listing_filters">
            <filterInput name="sku_list" template="ui/grid/filters/field" class="Acme\StackExchange\Ui\Component\Listing\Filter\Sku" provider="${ $.parentName }">
                <settings>
                    <dataScope>sku_list</dataScope>
                    <label translate="true">SKU (comma)</label>
                </settings>
            </filterInput>
        </filters>
    </listingToolbar>
</listing>

Result Applied filter

6
  • Hello @Victor, thank you for the response. Commented Jun 5, 2022 at 3:57
  • I will test it & let you know if that works, is it works for you ? Commented Jun 5, 2022 at 3:57
  • I am using default Magento's ui component & displaying data in admin grid. I have other filters too which are also custom, is there any possibility to manage them also with the file you have sent? Commented Jun 5, 2022 at 3:59
  • I have tested its not working, it shows no record when comma separated SKU are added to filter. Commented Jun 5, 2022 at 9:16
  • Provided example is tested and it's work in 2.4.4 with product grid. Please to be sure you've properly use example and you don't keep filter for column. Please update question with your custom listing XML and provide an example of product sku for filter you are use. Is your collection has sku as SKU field? Commented Jun 5, 2022 at 11:18

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.