Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

magento

I want to echo on my homepage the total sales of a product. How can this be done?

Also i would like to know how to query Total items sold (quantity) on the front page.

In my shop, there will be only 1 product (virtual).

Edit:

I found this code and it works pretty well in the product's view page.

$sku = nl2br($_product->getSku());
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToFilter('sku', $sku)
    ->setOrder('ordered_qty', 'desc')
    ->getFirstItem();
$product = $_productCollection;

echo 'Already Bought '.(int)$product->ordered_qty; 

But on the front page, how would I point directly to the ID of the product i want?

like image 898
Johny_Diuwep Avatar asked Oct 18 '11 13:10

Johny_Diuwep


2 Answers

This should work:

$id = 123; // enter your product ID here
$product = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToFilter('id', $id)
    ->setOrder('ordered_qty', 'desc')
    ->getFirstItem();

echo 'Already Bought '.(int)$product->ordered_qty;
like image 93
vzwick Avatar answered Nov 06 '22 12:11

vzwick


//This will show  total sales of all the products 
<?php    
     foreach(Mage::getModel('catalog/product')->getCollection() as $product)
     {
            $productId= $product->getId();
            $productModel = Mage::getModel('catalog/product');
            $name = $productModel->load($productId)->getName();
            echo "<br/>";
            $product = Mage::getResourceModel('reports/product_collection')
                ->addOrderedQty()
                ->addAttributeToFilter('entity_id', array('eq' => $productId))
                ->setOrder('ordered_qty', 'desc')
                ->getFirstItem();
            echo (int)$product->ordered_qty.'&nbsp'.$name.' Have Been Sold';
        }
?> 
like image 3
usman siddiqui Avatar answered Nov 06 '22 11:11

usman siddiqui