How to get total no of sold products in magento

How to get total no of sold products in magento

October 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get total no of sold products in magento

Getting the Total Number of Sold Products in Magento

Magento is a powerful e-commerce platform that allows you to manage your online store’s operations effectively. One common requirement is to get the total number of products sold across all orders. In this guide, we’ll walk through two primary methods to achieve this.

Method 1: Using Magento’s Sales Module

The Sales module in Magento provides APIs and tables that allow you to retrieve order data, including the number of items ordered. Here’s a step-by-step process:

  1. First, enable the Magento API consumer if it’s not already active. To do this, log into your admin panel, navigate to System > Web Services > SOAP XML-RPC Consumer, and set the ‘Consumer’ field to ‘Yes’. Save the settings.

  2. Now, you can use the sales_order_collection collection to retrieve order data. Here’s an example PHP script:

require_once 'app/Mage.php';
Mage::init();

$orderCollection = Mage::getModel('sales/order')->getCollection();
$soldProductsTotal = 0;

foreach ($orderCollection as $order) {
    foreach ($order->getAllItems() as $item) {
        $soldProductsTotal += $item->getQtyOrdered();
    }
}

echo "Total sold products: " . $soldProductsTotal;

This script initializes the Magento application and retrieves all orders using the sales_order_collection. It then iterates through each order’s items, summing up the ordered quantity for each product. The total is then displayed.

Method 2: Directly Querying the Database

For a more direct approach, you can query the sales_flat_order_item table directly in your MySQL database. Here’s how:

SELECT SUM(qty_ordered) as total_sold_products FROM sales_flat_order_item;

This SQL query calculates the sum of the qty_ordered field for all items across all orders.

Conclusion

Both methods allow you to retrieve the total number of sold products in your Magento store. The API method is more flexible and can be integrated into custom scripts or modules, while the direct database query offers a simpler, one-time solution.

Remember that working with Magento’s data directly requires careful handling to avoid conflicts and inconsistencies. Always ensure your store is backed up before making significant changes.

For a comprehensive overview of Magento development practices and best strategies for managing your e-commerce platform, I recommend exploring the resources available at Laravel Company. They provide in-depth guides on everything from performance optimization to custom theme development.

In your next blog post, you might explore how to get detailed information about each sold product, which would require a more complex query involving multiple tables.

Catalog Database API Extensions

Our network