0

I am trying to add working filters for max_sale_qty and manage_stock in product grid in Magento 2.3. I got the columns Max Sale Qty and Manage Stock in product grid to display the records using addField() methods and dataSource/dataProvider. But I couldn't get the filters to work though. I am fairly new to Magento and couldn't get the filters to work after hours of looking up online.

How do I get these filters working? Thanks.

Here is my code (All the required module files are given):

VendorName/ProductMaxSaleQty/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">

    <!-- datasource that has a dataprovider -->
    <dataSource name="product_listing_data_source" component="Magento_Ui/js/grid/provider">
      
        <settings>
            <storageConfig>
                <param name="dataScope" xsi:type="string">filters.store_id</param>
            </storageConfig>
            <updateUrl path="mui/index/render"/>
        </settings>

        <aclResource>Magento_Catalog::products</aclResource>


        <!-- dataprovider -->
        <dataProvider class="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider" name="product_listing_data_source">

            <settings>
                <requestFieldName>id</requestFieldName>
                <primaryFieldName>entity_id</primaryFieldName>
            </settings>

        </dataProvider>

    </dataSource>


    <!-- defining the columns -->
    <columns name="product_columns">

        <column name="manage_stock" component="Magento_Ui/js/grid/columns/select" sortOrder="76">
                <settings>
                    <addField>true</addField>
                    <options class="Magento\Config\Model\Config\Source\Yesno"/>
                    <filter>select</filter>
                    <dataType>select</dataType>
                    <sortable>false</sortable>
                    <label translate="true">Managed Stock</label>
                </settings>
           </column>

        <column name="max_sale_qty" sortOrder="77">
            <settings>
                <addField>true</addField>
                <filter>textRange</filter>
                <label translate="true">Max Sale Qty</label>
            </settings>
        </column>

    </columns>

</listing>

VendorName/ProductMaxSaleQty/etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider">
        <arguments>

            <argument name="addFieldStrategies" xsi:type="array">
                <item name="max_sale_qty" xsi:type="object">VendorName\ProductMaxSaleQty\Ui\DataProvider\Product\AddMaxSaleQtyFieldToCollection</item>
            </argument>
            <argument name="addFilterStrategies" xsi:type="array">
                <item name="max_sale_qty" xsi:type="object">VendorName\ProductMaxSaleQty\Ui\DataProvider\Product\AddMaxSaleQtyFilterToCollection</item>
            </argument>

            <argument name="addFieldStrategies" xsi:type="array">
                <item name="manage_stock" xsi:type="object">VendorName\ProductMaxSaleQty\Ui\DataProvider\Product\AddManageStockFieldToCollection</item>
            </argument>
            <argument name="addFilterStrategies" xsi:type="array">
                <item name="manage_stock" xsi:type="object">VendorName\ProductMaxSaleQty\Ui\DataProvider\Product\AddManageStockFilterToCollection</item>
            </argument>

        </arguments>
    </type>
</config>

VendorName/ProductMaxSaleQty/Ui/DataProvider/Product/AddManageStockFieldToCollection.php

<?php

namespace VendorName\ProductMaxSaleQty\Ui\DataProvider\Product;

class AddManageStockFieldToCollection implements \Magento\Ui\DataProvider\AddFieldToCollectionInterface
{
    public function addField(\Magento\Framework\Data\Collection $collection, $field, $alias = null)
    {
        $collection->joinField('manage_stock', 'cataloginventory_stock_item', 'manage_stock', 'product_id=entity_id', null, 'left');
    }
}

VendorName/ProductMaxSaleQty/Ui/DataProvider/Product/AddManageStockFilterToCollection.php

<?php

namespace VendorName\ProductMaxSaleQty\Ui\DataProvider\Product;

class AddManageStockFilterToCollection implements \Magento\Ui\DataProvider\AddFilterToCollectionInterface
{

    public function addFilter(\Magento\Framework\Data\Collection $collection, $field, $condition = null)
    {
        if (isset($condition['eq'])) {
            $collection->addFieldToFilter($field, $condition);

        }                   
    } 
}

VendorName/ProductMaxSaleQty/Ui/DataProvider/Product/AddMaxSaleQtyFieldToCollection.php

<?php

namespace VendorName\ProductMaxSaleQty\Ui\DataProvider\Product;

class AddMaxSaleQtyFieldToCollection implements \Magento\Ui\DataProvider\AddFieldToCollectionInterface
{
    public function addField(\Magento\Framework\Data\Collection $collection, $field, $alias = null)
    {
        $collection->joinField('max_sale_qty', 'cataloginventory_stock_item', 'max_sale_qty', 'product_id=entity_id', null, 'left');
    }
}

VendorName/ProductMaxSaleQty/Ui/DataProvider/Product/AddMaxSaleQtyFilterToCollection.php

<?php

namespace VendorName\ProductMaxSaleQty\Ui\DataProvider\Product;

use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection as AttributesCollection;
use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFilterToCollectionInterface;

class AddMaxSaleQtyFilterToCollection implements \Magento\Ui\DataProvider\AddFilterToCollectionInterface
{
    public function addFilter(\Magento\Framework\Data\Collection $collection, $field, $condition = null)
    {
        if (isset($condition['eq'])) {
            $collection->addFieldToFilter($field, $condition);
        }
    }
}

VendorName/ProductMaxSaleQty/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="VendorName_ProductMaxSaleQty" setup_version="0.0.1">
    <sequence>
      <module name="Magento_Catalog"/>
    </sequence>
  </module>
</config>

VendorName/ProductMaxSaleQty/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'VendorName_ProductMaxSaleQty',
    __DIR__
);

1 Answer 1

0

Looks like the logic inside these files make it not work.

Pls try changing them as following;

VendorName/ProductMaxSaleQty/Ui/DataProvider/Product/AddManageStockFilterToCollection.php

<?php

namespace VendorName\ProductMaxSaleQty\Ui\DataProvider\Product;

use Magento\Eav\Model\Entity\Collection\AbstractCollection;

class AddManageStockFilterToCollection implements \Magento\Ui\DataProvider\AddFilterToCollectionInterface
{

    public function addFilter(\Magento\Framework\Data\Collection $collection, $field, $condition = null)
    {
//        if (isset($condition['eq'])) {
//            $collection->addFieldToFilter($field, $condition);
//
//        }

//        if (isset($condition['in'])) {
//            $collection->getSelect()->where(
//                AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX .  'qty.manage_stock IN (?)',
//                implode($condition['in'])
//            );
//        }

        if (isset($condition['eq'])) {
                $collection->getSelect()->where(
                    AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX . 'qty.manage_stock = ?',
                    (float)$condition['eq']
                );
        }
    }
}

VendorName/ProductMaxSaleQty/Ui/DataProvider/Product/AddMaxSaleQtyFilterToCollection.php

<?php

namespace VendorName\ProductMaxSaleQty\Ui\DataProvider\Product;

use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Attribute\Collection as AttributesCollection;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Framework\Data\Collection;
use Magento\Ui\DataProvider\AddFilterToCollectionInterface;

class AddMaxSaleQtyFilterToCollection implements \Magento\Ui\DataProvider\AddFilterToCollectionInterface
{
    public function addFilter(\Magento\Framework\Data\Collection $collection, $field, $condition = null)
    {
//        if (isset($condition['eq'])) {
//            $collection->addFieldToFilter($field, $condition);
//        }
        if (isset($condition['gteq'])) {
            $collection->getSelect()->where(
                AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX . 'qty.max_sale_qty >= ?',
                (float)$condition['gteq']
            );
        }
        if (isset($condition['lteq'])) {
            $collection->getSelect()->where(
                AbstractCollection::ATTRIBUTE_TABLE_ALIAS_PREFIX . 'qty.max_sale_qty <= ?',
                (float)$condition['lteq']
            );
        }
    }
}

3
  • Thanks for your answer. Filter for max_sale_qty is now working, cheers! But manage_stock filter was previously not working, no errors. I used xdebug to find out its yes/no dropdown is sending 1/0 and made few changes to the filter code to get it working. Modified your answer with my code and everything works now. Please review and accept my edit. Cheers.
    – Raja P
    Dec 21, 2020 at 12:33
  • @RajaP what happens if both Yes and No options are selected, was just curios, I couldn't check it after your fix though
    – rukrlf
    Dec 22, 2020 at 8:53
  • The manage_stock filter I have added in product_listing.xml is a dropdown selector and since being not a multiselector one cannot select both Yes and No, so no issues there :)
    – Raja P
    Dec 23, 2020 at 2:36

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.