Magento 2's Decimal Dilemma: Unpacking the Order Status Bug with Fractional Quantities
Magento 2's Decimal Dilemma: Unpacking the Order Status Bug with Fractional Quantities
As e-commerce migration experts at Shopping Mover, we constantly monitor the Magento ecosystem for critical issues that can impact merchants and developers. A recent GitHub issue (#41749) has highlighted a significant bug in Magento 2.4.x concerning order status management when dealing with decimal quantities. This insight delves into the problem, community discussions, and potential solutions.
The Core Problem: Premature Order Completion
The issue, reported by annukka-tormala, describes a scenario where orders containing products with decimal quantities (e.g., 0.5 units, 1.5 units) are incorrectly marked as 'complete' immediately upon invoicing. This happens even when no shipment has been created, or only a partial shipment has occurred. The expected behavior is for the order to remain in a 'processing' state until all ordered items have been shipped.
The bug specifically manifests under these conditions:
- Magento version 2.4.9 (confirmed on 2.4.x branches).
- A simple product has "Qty Uses Decimals" set to "Yes" in its Advanced Inventory settings.
- A decimal quantity of the product is added to the cart and ordered.
- The order is placed and immediately invoiced (e.g., via certain payment methods).
In such cases, despite qty_shipped remaining at 0 or less than qty_ordered, the order status transitions directly to 'complete'. This can lead to significant operational challenges, including confusion in fulfillment workflows and inaccurate reporting of order statuses.
Community Insights and Potential Root Causes
While the GitHub issue itself primarily serves as a bug report, the Magento community often quickly mobilizes to identify root causes and propose workarounds. Discussions around similar issues typically point towards the core logic within Magento's sales module responsible for determining order status transitions. Specifically, the methods that evaluate whether an order can be marked 'complete' (e.g., within Mage\Sales\Model\Order or related classes) may contain flaws when comparing quantities that involve floating-point numbers.
Developers speculate that the issue might stem from:
- Floating-point precision errors: Comparisons between
qty_ordered,qty_invoiced, andqty_shippedmight not correctly handle decimal values, potentially leading to incorrect evaluations. - Implicit type casting: A comparison might inadvertently cast decimal values to integers, causing 0.5 to be treated as 0, or 1.5 as 1, leading to an early 'complete' status if the integer parts match.
- Incomplete checks: The logic for determining completion might not fully account for the
qty_shippedvalue when decimal quantities are involved, especially if the invoice amount matches the order total but shipment is pending.
Workarounds and Solutions
For merchants and developers facing this critical bug, immediate workarounds are crucial. The most common community-suggested approach involves creating a custom observer to intercept the order status change after invoicing and correct it if necessary.
Proposed Workaround (Custom Observer Example):
A custom module could implement an observer on the sales_order_invoice_save_after event. This observer would check the order's quantities and, if qty_shipped is less than qty_ordered (and decimal quantities are involved), it would reset the order status to 'processing'.
// app/code/ShoppingMover/OrderFix/etc/events.xml
// app/code/ShoppingMover/OrderFix/Observer/InvoiceSaveAfter.php
getEvent()->getInvoice();
/** @var \Magento\Sales\Model\Order $order */
$order = $invoice->getOrder();
// Check if the order has decimal quantities and is prematurely complete
if ($order->getState() === Order::STATE_COMPLETE && $order->canShip()) {
$hasDecimalQty = false;
foreach ($order->getAllItems() as $item) {
if ($item->getQtyOrdered() != floor($item->getQtyOrdered())) {
$hasDecimalQty = true;
break;
}
}
// If decimal quantities are present and nothing is shipped, revert to processing
if ($hasDecimalQty && $order->getTotalQtyShipped() < $order->getTotalQtyOrdered()) {
$order->setState(Order::STATE_PROCESSING)
->setStatus(Order::STATE_PROCESSING)
->save();
}
}
}
}
This observer ensures that if an order with decimal quantities is prematurely marked complete, and there are still items to be shipped, its status is corrected to 'processing'. This provides a temporary fix until Adobe Commerce/Magento Open Source releases an official patch.
Impact and Recommendations
This bug, confirmed on Magento 2.4.x, is a P2 priority issue, indicating its significant impact on non-critical functionality that requires a workaround. For merchants utilizing decimal quantities (common in industries like textiles, bulk goods, or custom orders), this can lead to fulfillment errors and customer dissatisfaction. Developers should be aware of this behavior and consider implementing the described workaround or similar logic to safeguard order integrity. Staying updated with Magento releases and applying official patches as they become available is always the best long-term solution.