Site icon EcomPlugins Blog

Magento 2 how to create shipment from different shipping method than customer ordered shipping method.

To create a shipment with a different shipping method than the one originally ordered, and allow selection from a dropdown of all available shipping methods, you need to customize the Magento 2 admin panel and extend its functionality. Here is a step-by-step guide to achieve this:

Step 1: Create a Custom Module

Assuming you have some familiarity with Magento 2 module development, create a custom module named Vendor_CustomShipment.

Folder Structure:

app/code/Vendor/CustomShipment/
├── registration.php
├── etc
│   └── module.xml
├── view
│   └── adminhtml
│       └── ui_component
│           └── sales_order_shipment_new.xml
└── Plugin
    └── ShipmentPlugin.php

registration.php:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_CustomShipment',
    __DIR__
);

module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_CustomShipment" setup_version="1.0.0">
    </module>
</config>

Step 2: Extend the Shipment Creation Form

Create the file sales_order_shipment_new.xml to extend the shipment creation form and add a dropdown for selecting a shipping method.

sales_order_shipment_new.xml:

<?xml version="1.0"?>
<page xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/ui_configuration.xsd">
    <update handle="sales_order_shipment_new"/>
    <container name="custom_shipment" htmlTag="div" htmlClass="custom-shipment">
        <uiComponent name="sales_order_shipment_new">
            <argument name="data" xsi:type="array">
                <item name="js_config" xsi:type="array">
                    <item name="provider" xsi:type="string">shipment_provider</item>
                    <item name="deps" xsi:type="string">shipment_provider</item>
                </item>
                <item name="config" xsi:type="array">
                    <item name="dataScope" xsi:type="string">shipment</item>
                    <item name="deps" xsi:type="string">shipment_provider</item>
                </item>
            </argument>
            <settings>
                <dataSource name="sales_order_shipment_new_data_source"/>
                <dataForm name="sales_order_shipment_new_data_form">
                    <fieldset name="shipment_fields">
                        <field name="shipping_method">
                            <argument name="data" xsi:type="array">
                                <item name="config" xsi:type="array">
                                    <item name="label" xsi:type="string" translate="true">Shipping Method</item>
                                    <item name="dataType" xsi:type="string">text</item>
                                    <item name="formElement" xsi:type="string">select</item>
                                    <item name="source" xsi:type="string">shipment</item>
                                    <item name="dataScope" xsi:type="string">custom_shipping_method</item>
                                    <item name="options" xsi:type="array">
                                        <item name="0" xsi:type="array">
                                            <item name="value" xsi:type="string">carrier_code_method_code</item>
                                            <item name="label" xsi:type="string" translate="true">Carrier - Method</item>
                                        </item>
                                    </item>
                                </item>
                            </argument>
                        </field>
                    </fieldset>
                </dataForm>
            </settings>
        </uiComponent>
    </container>
</page>

Step 3: Create a Plugin to Override the Shipment Creation

Create a plugin to intercept the shipment creation process and apply the selected shipping method.

ShipmentPlugin.php:

<?php
namespace Vendor\CustomShipment\Plugin;

use Magento\Sales\Api\Data\ShipmentInterface;
use Magento\Sales\Api\ShipmentRepositoryInterface;
use Magento\Framework\App\RequestInterface;

class ShipmentPlugin
{
    protected $request;

    public function __construct(RequestInterface $request)
    {
        $this->request = $request;
    }

    public function beforeSave(
        ShipmentRepositoryInterface $subject,
        ShipmentInterface $shipment
    ) {
        $customShippingMethod = $this->request->getParam('custom_shipping_method');
        
        if ($customShippingMethod) {
            $shipment->getOrder()->setShippingMethod($customShippingMethod);
        }

        return [$shipment];
    }
}

Step 4: Enable and Test the Module

Enable the module:

php bin/magento module:enable Vendor_CustomShipment
php bin/magento setup:upgrade
php bin/magento cache:flush

Test the Shipment Creation:

  1. Create an order via the Magento frontend or admin panel.
  2. Go to the order in the admin panel and create a shipment.
  3. In the shipment creation form, you should see the dropdown for selecting a shipping method.
  4. Select the desired shipping method and create the shipment.

Explanation

This approach provides a user-friendly way to select a different shipping method during the shipment creation process, enhancing the flexibility and functionality of the Magento 2 admin panel.

Exit mobile version