Magento 2 Order Processing

🧾 Magento 2 Order Processing – From Checkout to Complete

Magento 2 provides a structured way to handle orders from checkout to shipment and completion. Whether you’re automating order handling or customizing workflows, it’s key to understand how order processing works internally.

🛒 Step-by-Step Order Flow in Magento 2

  1. Customer places an order
  2. Order is saved with status pending
  3. Admin/integration generates an invoice (order status becomes processing)
  4. Shipment is created (optional depending on fulfillment)
  5. Order is marked complete once invoiced and shipped

📦 Load Order by Increment ID

$order = $this->orderRepository->get($orderId);
// or by increment ID
$order = $this->orderRepository->getList(
    $this->searchCriteriaBuilder
        ->addFilter('increment_id', '000000123')
        ->create()
)->getFirstItem();

Try It Now

🧾 Create an Invoice Programmatically

if ($order->canInvoice()) {
    $invoice = $invoiceService->prepareInvoice($order);
    $invoice->register();
    $invoice->save();

    $transaction = $transactionFactory->create()
        ->addObject($invoice)
        ->addObject($invoice->getOrder());
    $transaction->save();
}

Try It Now

🚚 Create a Shipment Programmatically

if ($order->canShip()) {
    $shipment = $shipmentFactory->create($order, $order->getAllItems());
    $shipment->register();
    $shipment->getOrder()->setIsInProcess(true);

    $transaction = $transactionFactory->create()
        ->addObject($shipment)
        ->addObject($shipment->getOrder());
    $transaction->save();
}

Try It Now

❌ Cancel an Order

if ($order->canCancel()) {
    $order->cancel();
    $order->save();
}

Try It Now

🔁 Full Order Lifecycle Recap

  • Pending – Order placed but not invoiced
  • Processing – Invoice created
  • Complete – Order shipped and invoiced
  • Canceled – Order canceled manually or automatically

This flow can be customized via plugins, observers, or service contracts to integrate with ERP systems, shipping APIs, or CRMs.

Magento 2’s order processing is powerful and modular – once you understand the lifecycle, automation becomes easy!