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

I want the total sales of the product to be displayed on my homepage. How can I do that?

I would also like to know how to request the total number of items sold (quantity) on the first page.

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

Edit

I found this code and it works well on the product 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 first page, how would I point directly to the product identifier I want?

+4
source share
4 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; 
+4
source
 //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'; } ?> 
+1
source

The code should be as follows:

 $id = $_product->getId(); $product = Mage::getResourceModel('reports/product_collection') ->addOrderedQty() ->addAttributeToFilter('entity_id', array('eq' => $id)) ->setOrder('ordered_qty', 'desc') ->getFirstItem(); echo 'Downloaded '.(int)$product->ordered_qty.'times'; 

this code works like me (show how many times, curent product has been sold)

0
source

Show product by maximum product sold on listing page

 $i=0; foreach($_collection as $product) { $productId= $product->getId(); $productModel = Mage::getModel('catalog/product'); $name = $productModel->load($productId)->getName(); $product1 = Mage::getResourceModel('reports/product_collection') ->addOrderedQty() ->addAttributeToFilter('entity_id', array('eq' => $productId)) ->setOrder('ordered_qty', 'desc') ->getFirstItem(); $solditems[$i]= array($product1->ordered_qty,$product); $i++; } rsort($solditems); print_r($solditems); 
0
source

All Articles