Thursday, June 30, 2011

How to get sold quantity of product in magento


If you want to have it so that it only shows quantity sold on the product page on items that are currently on sale.. use the following., There will also be an alert for customers when there is only 1 remaining in stock.Then use the following code.
<?php 
$_finalPrice 
$this->helper('tax')->getPrice($_product$_product->getFinalPrice());$_regularPrice $this->helper('tax')->getPrice($_product$_product->getPrice());
if (
$_regularPrice != $_finalPrice):$sku nl2br($_product->getSku());$to $_product->getResource()->formatDate(time());$from $_product->getResource()->formatDate(time() - 60 60 24 1);$_productCollection Mage::getResourceModel('reports/product_collection')
->
addOrderedQty($from$totrue)
->
addAttributeToFilter('sku'$sku)
->
setOrder('ordered_qty''desc')
->
getFirstItem();$product $_productCollection;

echo 
'Already Bought Today '.(int)$product->ordered_qty
endif;
?>
<?php 
if ((int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()==1): ?><p style="color:#990000; padding:5px 0; text-align:right;"><strong>ONLY 1 LEFT IN STOCK!</strong></p><?php endif; ?>

Or if you want to show stock sold on the product page for the entire duration the product has been active, use the following
<?php 

$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?>

and finally if you want to just just on the page how many have been sold of the product for today, use the following

<?php 
$sku 
nl2br($_product->getSku());$to $_product->getResource()->formatDate(time());$from $_product->getResource()->formatDate(time() - 60 60 24 1);$_productCollection Mage::getResourceModel('reports/product_collection')
->
addOrderedQty($from$totrue)
->
addAttributeToFilter('sku'$sku)
->
setOrder('ordered_qty''desc')
->
getFirstItem();$product $_productCollection;

echo 
'Quantity Ordered Today '.(int)$product->ordered_qty
endif;
?>

Just copy and paste into your view.phtml file, into a suitable place :)

No comments:

Post a Comment