Magento: How to display how many times a product has been sold?

Magento: How to display how many times a product has been sold?

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: How to display how many times a product has been sold?

Displaying Total Sales of a Product in Magento

As a Magento developer, I often get asked how to display the total sales or quantity sold for a specific product on the front page. This is particularly useful when you want to showcase popular items or create a sense of urgency with limited stock.

The first step is to understand that Magento uses the sales_flat_order_item table to store information about each item in an order, including the product ID and quantity sold. We can use this table to query the total sales for any given product.

Querying Total Sales for a Specific Product

To achieve this, you’ll need to create a custom block or helper function that queries the database directly using Magento’s resource model. Here’s a step-by-step guide:

  1. Create a new Magento block file (e.g., My_Module_Block_DisplaySales.php).

  2. In your new block, use the Mage::getResourceModel('sales/order_item_collection') to create a collection of order items.

  3. Apply an attribute filter for the product ID using $collection->addAttributeToFilter('product_id', $productId);.

  4. Retrieve the sum of the quantity sold by calling array_sum($collection->getColumnValues('qty_ordered'));. This will give you the total number of units sold for that product.

Here’s a sample code snippet:

public function getTotalSalesForProduct($productId)
{
    $collection = Mage::getResourceModel('sales/order_item_collection');
    $collection->addAttributeToFilter('product_id', $productId);

    return array_sum($collection->getColumnValues('qty_ordered'));
}

Displaying Total Sales on the Front Page

Now, to display this information directly on your homepage, you’ll need to use the product ID of the item you want to showcase. You can either hardcode the ID or fetch it from a different model.

If your shop only has one virtual product, as mentioned in your question, you can simply get the first product’s ID using $product = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('type_id', 'virtual')->getFirstItem();.

Then, call our previously created block method:

$block = Mage::app()->getLayout()->createBlock('my_module/block_displaySales');
echo $block->getTotalSalesForProduct($product->getId());

Querying Total Items Sold for All Products

If you want to display the total items sold for all products on your homepage, you can modify the previous code snippet slightly:

public function getTotalSalesForAllProducts()
{
    $collection = Mage::getResourceModel('sales/order_item_collection');

    return array_sum($collection->getColumnValues('qty_ordered'));
}

This will give you the total number of units sold across all products in your store.

Conclusion

Displaying the total sales or quantity sold for a product is a powerful feature that can enhance your site’s user experience. By using Magento’s built-in resource models and collections, we can easily query this data and display it anywhere on our site, including the homepage.

Remember to always test your changes thoroughly before deploying them to a live environment. If you’d like to learn more about Magento development best practices or see how Laravel Company can help optimize your e-commerce platform, feel free to visit https://laravelcompany.com.

Catalog Database Extensions Orders

Our network