Magento Get total ordered items' quantity in success page

Magento Get total ordered items' quantity in success page

July 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento Get total ordered items' quantity in success page

Enhancing Your Magento Shopping Experience: Retrieving Total Ordered Item Quantities

In today’s fast-paced e-commerce landscape, providing a seamless shopping experience on your Magento store is crucial. One key metric that customers often look for after completing an order is the total quantity of items they’ve ordered.

If you’re facing challenges in retrieving this information on the success page, don’t worry—you’re not alone! In this comprehensive guide, we’ll walk through a step-by-step process to help you achieve this goal. Let’s dive right in!

Understanding the Challenge

Before we get started, let’s first clarify why this task might be tricky in Magento.

The shopping cart and order management systems in Magento are designed with different flows in mind. While it’s straightforward to access item quantities during the cart process or when viewing an order in the admin panel, reaching these details on the success page requires a bit of creative problem-solving.

Retrieving Order Data on the Success Page

The primary reason why retrieving item quantities is challenging lies in the way Magento handles redirection. When a customer places an order and is redirected to the success page, the order object isn’t readily available anymore.

This means you can’t simply call the getItemsQty method directly within your success.html file. Instead, you’ll need to fetch the relevant data from the database using a model or even directly query it if necessary.

Step-by-Step Guide: Accessing Total Ordered Item Quantities

Now that we’ve established the context let’s proceed with our step-by-step guide:

1. Fetching the Last Placed Order ID

First, you’ll need to retrieve the order ID of the recently placed order. Since Magento stores the last placed order ID in the session after redirection, you can access it using the following code snippet:

<?php
$lastOrderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
?>

2. Loading the Order Object

With the order ID at hand, you can now load the corresponding order object using Magento’s Mage_Sales_Model_Order model:

<?php
$order = Mage::getModel('sales/order')->load($lastOrderId);
?>

3. Calculating Total Quantity

Finally, to calculate the total quantity of all ordered items, you can iterate through the order items and sum up their quantities:

<?php
$itemsQty = 0;
foreach ($order->getAllVisibleItems() as $item) {
    $itemsQty += $item->getQtyOrdered();
}
?>

Displaying Total Quantity on Success.html

Once you have calculated the total quantity, you can display it directly within your success.html file using a custom block. Here’s an example of how to do this:

<?php
$block = Mage::app()->getLayout()->createBlock('core/text')
    ->setText('Total Quantity Ordered: ' . $itemsQty);
echo $block->toHtml();
?>

Alternative Approach: Customizing the Success Page Layout

If you prefer a more structured approach, you might want to consider creating a custom layout update XML file for your success page. This allows you to define a new block that fetches and displays the total quantity directly within the layout definition.

First, create an XML file under app/design/frontend/your_package/your_theme/layout/success.xml:

<?xml version="1.0"?>
<layout version="1.0">
    <success_index_index>
        <reference name="content">
            <block type="core/text" name="total_ordered_qty" as="total_ordered_qty">
                <action method="setText">
                    <text helper="sales/order_totals/getSubtotalLabel"/>
                </action>
            </block>
        </reference>
    </success_index_index>
</layout>

Then, create a custom block file under app/code/local/YourCompany/Sales/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <YourCompany_Sales>
            <version>1.0.0.0</version>
        </YourCompany_Sales>
    </modules>
    <global>
        <models>
            <sales>
                <class>YourCompany_Sales_Model</class>
            </sales>
        </models>
    </global>
</config>

Finally, create the block file under app/code/local/YourCompany/Sales/Block/Order/Totals.php:

<?php
class YourCompany_Sales_Block_Order_Totals extends Mage_Core_Block_Text {
    public function _toHtml() {
        $order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastRealOrderId());

        if ($order) {
            $itemsQty = 0;
            foreach ($order->getAllVisibleItems() as $item) {
                $itemsQty += $item->getQtyOrdered();
            }

            return 'Total Quantity Ordered: ' . $itemsQty;
        } else {
            return '';
        }
    }
}
?>

Conclusion

Retrieving the total quantity of ordered items on your Magento success page requires a bit of creative problem-solving and understanding of how Magento handles data flow during redirection.

In this comprehensive guide, we’ve explored two primary approaches to achieve this task: using direct PHP code within the success.html file and creating a custom layout update XML file with an associated block class.

Both methods allow you to fetch the required data from the database and display it on your success page seamlessly. The choice between these approaches depends on your preferred coding style and whether you want to keep your HTML files clean or maintain a more structured layout system.

Remember, Magento is a powerful e-commerce platform that offers numerous customization options. By leveraging its flexibility and understanding its underlying architecture, you can create engaging shopping experiences for your customers while keeping them informed about their orders every step of the way.

For more in-depth guides on Magento development and best practices, be sure to visit our blog at Laravel Company. We’re committed to helping developers like yourself unlock the full potential of this robust e-commerce platform.

Happy coding!

Checkout Database Themes Admin

Our network