Question

How to Cancel Shipment Creation for an Order in Magento2?

I have a condition where If the condition is false I need to stop Shipment Creation and Cancel the order. How can I manage this? Any help will be appreciated.

Was it helpful?

Solution

For this requirement, you have to work on Magento\Sales\Model\Order\Shipment::register() .

Create a plugin on Magento\Sales\Model\Order\Shipment::register(

Then using around plugin on register() implement your logic.

Create di.xml at your module and defined the plugin class of Magento\Sales\Model\Order\Shipment

<?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\Sales\Model\Order\Shipment">
        <plugin disabled="false" name="Devbera_PreventInvoice_Plugin_Magento_Sales_Model_Order_Shipment" sortOrder="10" type="{Vendorname}\{ModuleName}\Plugin\Magento\Sales\Model\Order\Shipment"/>
    </type>
</config>

And after that create a plugin class on app\code\{Vendorname}\{ModuleName}\Plugin\Plugin\Magento\Sales\Model\Order\Shipment.php,prevent the shipment create or cancel order or etc.

But note if you created any invoice of that order then you cannot cancel that order and you have to create a credit memo instead of cancel.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top