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.

  • Which condition you want to apply? – Dhaduk Mitesh Jan 7 '20 at 7:27
  • Hi mitesh, we have an attribute which says ready to ship or not, i need to check that attribute and cancel the shipment if attribute return false. – Prasanth Jan 7 '20 at 8:32
  • That means you want to hide ship button. Right? – Dhaduk Mitesh Jan 7 '20 at 8:37

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"/>

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.

  • hi Amit, thank you for your response.I need to stop the shipment creation, which method i need to use to stop the shipment. – Prasanth Jan 7 '20 at 8:04
  • OOPS, it did the mistake give the answer on invoice – Amit Bera Jan 7 '20 at 8:11
  • The answer is update based on Shipment – Amit Bera Jan 18 '20 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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