1

I want to override product collection in Magento product grid in admin side.

enter image description here

Please let me know which file I need to override for collection filter

2 Answers 2

1

You can use Plugin and add below code in your admin's di.xml file

<type name="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider">
    <plugin name="product-collection-sorting" type="[Vendor]\[Module]\Plugin\ProductDataProvider" sortOrder="999"/>
</type>

Now create file [Vendor][Module]\Plugin\ProductDataProvider

<?php

namespace [Vendor]\[Module]\Plugin;

use Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider as RelatedProductDataProvider;


class ProductDataProvider extends RelatedProductDataProvider
{

public function afterGetData(
        \Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider $subject,
        $result
    )
    {
       
        $subject->getCollection()->clear();
        $subject->getCollection()->addFieldToFilter('attribute_set_id', ['eq' => 4])->load();
        $items = $subject->getCollection()->addFieldToFilter('attribute_set_id', ['eq' => 4])->toArray();
        $totalRecords = $subject->getCollection()->addFieldToFilter('attribute_set_id', ['eq' => 4])->load()->getSize();
    }

}
1

Recently i did similar by using prefernce
<preference for="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider" type="vendor\Module\Ui\DataProvider\Product\ProductDataProvider" />

2
  • It's showing me this error after overriding the file Notice: Undefined index: websites in /var/www/html/sterling/vendor/magento/module-catalog/Ui/Component/Listing/Columns/Websites.php on line 84 Exception in /var/www/html/sterling/vendor/magento/framework/App/ErrorHandler.php:61 Mar 18, 2021 at 3:08
  • I have added category filter to product grid by overriding same file. plz check link
    – Mehran
    Mar 18, 2021 at 4:55

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.