Managing order statuses in Magento 2 can sometimes be tricky, especially when the default workflow doesn’t align perfectly with your business needs. By default, Magento 2 updates the order status to ‘Complete’ once a shipment is submitted. However, there are scenarios where you might want to keep the order status as ‘Processing’ even after the shipment is created. This blog post will guide you through the steps to achieve this using a custom Magento 2 module.
Why Keep the Order Status as ‘Processing’?
Before diving into the technical details, let’s explore why you might want to keep the order status as ‘Processing’:
- Multiple Shipments: For businesses that manage multiple shipments per order, keeping the status as ‘Processing’ helps in tracking incomplete orders effectively.
- Custom Workflow Requirements: Your business may have specific workflows where an order should only be marked as complete once other criteria are met, not just shipment creation.
- Avoiding Premature Completion: Ensuring the order status remains ‘Processing’ can help in avoiding any premature actions that might be triggered by a ‘Complete’ status.
Step-by-Step Guide to Keep Order Status as ‘Processing’
To customize the order status behavior, we will create a custom Magento 2 module that listens to the sales_order_shipment_save_after
event and sets the order status back to ‘Processing’.
Step 1: Create a Custom Module
First, we need to create a custom module. Let’s name it Vendor_Module
.
Create the directory structure for the custom module:
app/code/Vendor/Module/
Create the module.xml
file:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0"/>
</config>
Create the registration.php
file:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Step 2: Create the Event Observer
Next, we will create an event observer to handle the sales_order_shipment_save_after
event.
Create the events.xml
file:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework/Event/etc/events.xsd">
<event name="sales_order_shipment_save_after">
<observer name="vendor_module_observer_sales_order_shipment_save_after" instance="Vendor\Module\Observer\SalesOrderShipmentSaveAfter" />
</event>
</config>
Create the Observer class:
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;
class SalesOrderShipmentSaveAfter implements ObserverInterface
{
public function execute(Observer $observer)
{
// Get the shipment
$shipment = $observer->getEvent()->getShipment();
// Get the order
$order = $shipment->getOrder();
// Set the order status back to 'processing'
$order->setStatus(Order::STATE_PROCESSING);
// Save the order
$order->save();
return $this;
}
}
Step 3: Enable and Deploy the Module
Finally, we need to enable and deploy the module.
Enable the module:
php bin/magento module:enable Vendor_Module
Run the setup upgrade script:
php bin/magento setup:upgrade
Deploy static content (if in production mode):
php bin/magento setup:static-content:deploy
Clear the cache:
php bin/magento cache:clean
Conclusion
By following the steps outlined above, you can easily customize Magento 2 to keep the order status as ‘Processing’ after a shipment is submitted. This customization ensures that your order management workflow aligns perfectly with your business requirements, providing better control over order statuses and avoiding premature completion of orders. Whether you manage multiple shipments per order or have specific workflow criteria, this solution is flexible enough to adapt to your needs.
Stay tuned for more Magento 2 tips and tricks to enhance your eCommerce capabilities!