0

I need to add a new field to shipment grid and this field must contain items price incl tax. So I also should plug into the shipment saving prosess to calculate and save this field value.

1 Answer 1

1

Add field shipment_cost to sales_shipment_grid and sales_shipment. Then:

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="shipment_cost">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Shipment Cost</item>
                </item>
            </argument>
        </column>
</columns>
</listing>

etc/di.xml

<virtualType name="ShipmentGridAggregator" type="Magento\Sales\Model\ResourceModel\Grid">
    <arguments>
        <argument name="columns" xsi:type="array">
            <item name="shipment_cost" xsi:type="string">sales_shipment.shipment_cost</item>
        </argument>
    </arguments>
</virtualType>

Observer\OrderShipmentSaveBefore.php

use Magento\Framework\Event\ObserverInterface;
class OrderShipmentSaveBefore implements ObserverInterface
{

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $shipment = $observer->getEvent()->getShipment();
        $items = $shipment->getItems();
        $result = 0;
        foreach($items as $item){
            $order_item = $item->getOrderItem();
            $item_cost=$order_item->getPriceInclTax()*$item->getQty();
            $result+=$item_cost;
        }
        $shipment->setData('shipment_cost',$result);
    }

}

Not the answer you're looking for? Browse other questions tagged or ask your own question.