Creating shipment does not update items shipped quantity and order is not complete

Creating shipment does not update items shipped quantity and order is not complete

August 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Creating shipment does not update items shipped quantity and order is not complete

Solving Magento Order Shipping Issues Programmatically

As a senior developer working on a Magento e-commerce platform, you may face challenges when trying to programmatically create shipments for orders that are already paid and invoiced. One common issue is that even after successfully creating the shipment, the order status remains at ‘processing’ instead of being marked as ‘complete’.

Identifying the Root Cause

The main problem lies in the fact that the quantity shipped for the order items is not updated correctly when saving the shipment and the associated order. As a result, Magento’s internal logic does not recognize the shipment as fully processed, keeping the order status at ‘processing’.

Analyzing the Provided Procedure

Let’s break down the provided procedure to understand where things go wrong:

  1. You’re creating an order collection with all attributes selected.
  2. You loop through each order in the collection and check if it can be shipped using canShip() method.
  3. If yes, you convert the order to a shipment using the _convertOrder object.
  4. You fetch all items from the converted order and filter out virtual or zero quantity items.
  5. For valid items, you create a shipment item with the required quantities and add it to the shipment.
  6. You register the shipment and set isInProcess flag on the associated order to true.
  7. The transaction factory is used to save both the shipment and the associated order.

The key issue here is that while the shipment items are being created and added, they are not actually being saved or committed back to the database.

The Solution Approach: Using Proper Transaction Commit

To resolve this issue, you need to ensure that the created shipment items and their associated transactions are properly saved and committed. This can be achieved by using Magento’s transaction management features more effectively:

  1. Create a composite transaction: Instead of adding individual objects to the transaction, create a composite transaction that includes both the shipment and its associated order.

  2. Set proper status on the order: After saving the shipment, explicitly set the order as ‘complete’ before committing the transaction.

  3. Catch and handle exceptions gracefully: If any exception occurs during the save operation, ensure you handle it correctly to prevent data inconsistency.

Updated Procedure with Corrections

Here’s an updated version of your procedure with the required changes:

// ... (previous code remains unchanged)

try {
    $shipment->getOrder()->setIsInProcess(true);
    $shipment->getOrder()->setStatus(Order::STATE_PROCESSING);
} catch (\Exception $e) {
    // Handle exceptions here to prevent data inconsistency
}

// Create a composite transaction for both shipment and its associated order
$saveTransaction = $this->_transactionFactory->create();
$saveTransaction->addObject($shipment)
                ->addObject($shipment->getOrder());

// Save the shipment and its associated order in a single transaction
try {
    $saveTransaction->save();

    // Set order status to 'complete' after successful save
    $shipment->getOrder()->setStatus(Order::STATE_COMPLETE)->save();
} catch (\Exception $e) {
    // Handle exceptions here for better error logging and recovery
}

Conclusion

Programmatically handling Magento shipments requires careful attention to the order status updates and database transaction management. By creating composite transactions that include both the shipment and its associated order, you can ensure that the quantity shipped is updated correctly and the order status reflects the actual shipping process.

Remember to handle exceptions properly during the save operation to prevent data inconsistency and maintain the integrity of your e-commerce system. If you need further assistance or want to explore advanced Magento development techniques, feel free to visit our website at https://laravelcompany.com for more resources and expert guidance.

Happy coding!

Orders Errors

Our network