Back to Top

Add custom column filter in sales credit memo grid collection in Magento 2

Updated 19 June 2024

Hello Friends!!!

In this blog, we are going to learn how we can add a custom column filter to the sales credit memo grid collection.
Here, to customize the sales credit memo grid collection, please follow the below steps:

1. Create di.xml inside the app/code/Vendor/Module/etc/ directory. And add the following plugin in your di.xml file.

<?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\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <plugin name="Vendor_Module::aroundGetReport" type="Vendor\Module\Plugin\Framework\View\Element\UiComponent\DataProvider\CollectionFactory" sortOrder="1"/>
  </type>

</config>

2. Create plugin class file CollectionFactory.php inside the app/code/Vendor/Module/Plugin/Framework/View/Element/UiComponent/DataProvider/ directory.

<?php
/**
 * Vendor Desc.
 *
 * @category  Vendor Category
 * @package   Vendor_Module
 * @author    Vendor name
 * @copyright Copyright (c) Vendor (https://example.com)
 * @license   https://example.com/license.html
 */

namespace Vendor\Module\Plugin\Framework\View\Element\UiComponent\DataProvider;

use Magento\Framework\Data\Collection;
use Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory as OrigFactory;

/**
 * Class CollectionFactory
 */
class CollectionFactory
{
    /**
     * Const SALES_ORDER_CREDITMEMO_GRID_DATA_SOURCE: contains source name
     */
    const SALES_ORDER_CREDITMEMO_GRID_DATA_SOURCE = 'sales_order_creditmemo_grid_data_source';

    /**
     * @var Collection[]
     */
    protected $collections;

    /**
     * @param array $collections
     */
    public function __construct(
        array $collections = []
    ) {
        $this->collections  = $collections;
    }

    /**
     * Get report collection
     *
     * @param OrigFactory $subject
     * @param \Closure $proceed
     * @param string $requestName
     * 
     * @return Collection
     * @throws \Exception
     */
    public function aroundGetReport(
        OrigFactory $subject,
        \Closure $proceed,
        $requestName
    ) {
        $result = $proceed($requestName);

        $userId = 2;

        if ($requestName == self::SALES_ORDER_CREDITMEMO_GRID_DATA_SOURCE) {
            //here user_id is custom column of sales_creditmemo and sales_creditmemo_grid tables
            $result->addFieldToFilter("main_table.user_id", $userId);
        }
        
        return $result;
    }
}

After executing the compilation command, You can see the result in the sales credit memo grid.

CreditMemoGrid
Credit Memos Grid Collection View

Hope this will be helpful.
Thanks 🙂

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home