1

I've already been able to add my column to the sales_order_shipment_grid. Now it's time to join the data from another table so we can display it.

Approach 1:

Some articles online suggest adding a join to the Grid resource model using something like:

<virtualType name="ShipmentGridAggregator">
    <arguments>
        <argument name="joins" xsi:type="array">
            <item name="my_table" xsi:type="array">
                <item name="table" xsi:type="string">my_table</item>
                <item name="origin_column" xsi:type="string">their_column</item>
                <item name="target_column" xsi:type="string">my_column</item>
            </item>
        </argument>
        <argument name="columns" xsi:type="array">
            <item name="my_column" xsi:type="string">my_table.my_other_column</item>
        </argument>
    </arguments>
</virtualType>

The problem with this (as far as I can tell), is that this is deprecated in favor of using UIComponents and DataProviders. The ShipmentGridAggregator is not used anywhere except for Magento\Sales\Model\ResourceModel\GridPool, which in itself is not used for anything useful.

Approach 2:

So, time to dig in deeper... The shipment grid uses a data source called sales_order_view_shipment_grid_data_source this in turn is configured to use Magento\Sales\Model\ResourceModel\Order\Shipment\Order\Grid\Collection. This class doesn't have an $_eventPrefix so we can't use an observer to join our field.

Aproach 3:

We are able to inject a custom FetchStrategyInterface into the collection listed above, but this doesn't seem to be its intended purpose. Will probably make it problematic with other modules that need to do something similar.

Approach 4:

The current plan... Just do a before-plugin on Magento\Sales\Model\ResourceModel\Order\Shipment\Order\Grid\Collection::load and add the join to getSelect() ourselves.

Anyone have any better ideas?

1 Answer 1

0

Review from: https://webkul.com/blog/add-custom-columns-to-order-grid-in-admin-magento-2/

Create or edit

app/code/[Vendor]/[Module_Name]/etc/admin/di.xml

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <plugin name="sales_order_shipment_columns_[unique_name]"
                type="[Vendor]/[Module_Name]\Ui\Component\Listing\Column\Filename"/>
    </type>

Create File at

/app/code/[Vendor]/[Module_Name]/Ui/Component/Listing/Column/Filename.php

namespace [Vendor]/[Module_Name]\Ui\Component\Listing\Column;
class Filename
{
   public static $table = 'sales_shipment_grid';
   public static $sales_order = 'sales_order';

   public function afterGetReport($subject, $collection, $requestName)
   {
      if ($requestName == 'sales_order_shipment_grid_data_source' || $requestName == 'sales_order_view_shipment_grid_data_source') {
        if ($collection->getMainTable() === $collection->getConnection()->getTableName(self::$table)) {
            $salesOrderTable = $collection->getConnection()->getTableName(self::$sales_order);
            $collection
                ->getSelect()
                ->joinLeft(
                    ['so' => $salesOrderTable],
                    "so.entity_id = main_table.order_id",
                    [
                        'so.column_name',
                    ]
                )
                ;
            return $collection;
        }
    }

    return $collection;
 }
}

Create file at

/app/code/[Vendor]/[Module_Name]/view/adminhtml/ui_component/sales_order_view_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="column_name">
                <settings>
                    <filter>text</filter>
                    <label translate="true">Column Title</label>
                </settings>
            </column>
        </columns>
    </listing>

Create File at

/app/code/[Vendor]/[Module_Name]/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="column_name">
                <settings>
                    <filter>text</filter>
                    <label translate="true">Column Title</label>
                </settings>
            </column>
        </columns>
    </listing>

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.