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?
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;
//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.' '.$name.' Have Been Sold';
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With