How to show source in shipment grid Magento 2 MSI

How to show source in shipment grid Magento 2 MSI

Hello Guys! Welcome back to another topic. Today I will show you how we can show supply source in shipment grid.

With default Magento we can see Shipment number, Ship Date, Order, Order Date, Total Quantity etc. in shipment grid.

To show the source of shipment we need to join inventory_shipment_source table to shipment grid data source and add new column in sales_order_shipment_grid.xml UI component

So we can add source column by three simple step

  1. Add plugin for sales_order_shipment_grid_data_source in di.xml
  2. Create a plugin to join inventory_shipment_source table to shipment grid data source
  3. Add new source column in sales_order_shipment_grid.xml

1. Add a plugin for sales_order_shipment_grid_data_source

app/code/MagePrince/Sales/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\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <plugin name="join_shioment_source_table_with_sales_shipment_grid"
                type="FastTimes\OrderRouting\Plugin\Sales\Shipment\UiComponent\DataProvider\CollectionFactory" sortOrder="10" />
    </type>
</config>

2. Create a plugin to join inventory_shipment_source table to shipment grid data source
app/code/MagePrince/Sales/Plugin/Sales/Shipment/UiComponent/DataProvider/CollectionFactory.php

<?php

namespace MagePrince\Sales\Plugin\Sales\Shipment\UiComponent\DataProvider;

use Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory as ParentCollectionFactory;
use Magento\Sales\Model\ResourceModel\Order\Shipment\Grid\Collection as ShipmentGridCollection;
use Magento\Sales\Model\ResourceModel\Order\Shipment\Order\Grid\Collection as ShipmentOrderGridCollection;

class CollectionFactory
{
    /**
     * @param ParentCollectionFactory $subject
     * @param \Closure $proceed
     * @param $requestName
     * @return mixed
     */
    public function aroundGetReport(
        ParentCollectionFactory $subject,
        \Closure $proceed,
        $requestName
    ) {
        $collection = $proceed($requestName);
        if ($requestName == 'sales_order_shipment_grid_data_source') {
            if ($collection instanceof ShipmentGridCollection) {
                $collection->getSelect()->joinLeft(
                    [
                        'source' => 'inventory_shipment_source'
                    ],
                    'source.shipment_id = main_table.entity_id',
                    '*'
                );
            }
        }

        return $collection;
    }
}

3. Add new source column in sales_order_shipment_grid.xml

app/code/MagePrince/Sales/view/adminhtml/ui_component/sales_order_shipment_grid.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">
    <columns name="sales_order_shipment_columns">
        <column name="source_code">
            <settings>
                <filter>text</filter>
                <label translate="true">Source</label>
            </settings>
        </column>
    </columns>
</listing>

With the above steps you can see new source column in the shipment grid like below

Hope this article will help you to show new source column in shipment grid.

If you have any question feel free to comment below. Thanks for reading.

Keep sharing and liking 🙂