Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DEV

Add custom column filter in sales shipment grid collection in Magento 2

Hello Friends!!!

In this blog, we are going to learn how we can add a custom column filter to the sales shipment grid collection.
Here, to customize the sales shipment 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_SHIPMENT_GRID_DATA_SOURCE: contains source name
     */
    const SALES_ORDER_SHIPMENT_GRID_DATA_SOURCE = 'sales_order_shipment_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_SHIPMENT_GRID_DATA_SOURCE) {
            //here user_id is custom column of sales_shipment and sales_shipment_grid tables
            $result->addFieldToFilter("main_table.user_id", $userId);
        }
        
        return $result;
    }
}

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

ShipmentsGrid
Customized Shipment Grid Collection View

Hope this will be helpful.
Thanks 🙂

. . .

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