how to count items in an order(when item status=Mixed) using orderid in magento?

how to count items in an order(when item status=Mixed) using orderid in magento?

August 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to count items in an order(when item status=Mixed) using orderid in magento?

Counting Items in a Mixed Order Status in Magento

As an experienced developer, I understand how challenging it can be to count items in a Magento order when some items are shipped while others remain unshipped or have mixed statuses. The default code you provided only counts the quantity of items slated for invoicing (getQtyToInvoice()), which may not include all items if their status is set as ‘mixed’.

Let’s improve this functionality by counting all order items, regardless of their current status.

Step 1: Load the Order

First, ensure you’re loading the order correctly using the order_id:

$oid = 123; // Replace with your actual order ID
$order = Mage::getModel('sales/order')->load($oid);

Step 2: Retrieve All Items

Next, retrieve all items in the order. The method getAllItems() returns an array of all items present in the order:

$items = $order->getAllItems();

Step 3: Count Total Quantity

Now, iterate through each item and calculate the total quantity. We’ll use getQtyOrdered() which provides the original quantity ordered by the customer:

$total = 0;
foreach ($items as $item) {
    $qty = $item->getQtyOrdered();
    $total += $qty;
}

Step 4: Display the Total

Finally, print or return the total count of items in the order. I prefer returning the value to maintain a clean and functional approach:

return "Total items in this order: " . $total;

Conclusion

By using getQtyOrdered(), you can now accurately calculate the total number of items in an order, even if some items have mixed statuses like ‘unshipped’ or ‘mixed’. This approach ensures your code remains robust and handles edge cases gracefully.

Remember to test this functionality thoroughly across different scenarios. If you require more complex calculations or need to integrate with other systems, consider exploring Magento’s APIs as discussed in our guide.

As a best practice, always keep your code clean and well-commented for easier maintenance and collaboration. Happy coding!

Disclaimer: The provided solution assumes you have the necessary permissions to access and modify Magento’s core files. Always backup your site before making changes, and consult with your system administrator if uncertain about any steps.

Themes Orders Customers

Our network