July 14, 2020

cm-mini

Thinking Magento

Show Quantity Sold On Product Page - 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.

$_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, $to, true)
->addAttributeToFilter('sku', $sku)
->setOrder('ordered_qty', 'desc')
->getFirstItem();

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

endif;

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());
 $product = Mage::getResourceModel('reports/product_collection')
 ->addOrderedQty()
 ->addAttributeToFilter('sku', $sku)
->setOrder('ordered_qty', 'desc')
->getFirstItem();

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);
$product = Mage::getResourceModel('reports/product_collection')
->addOrderedQty($from, $to, true)
->addAttributeToFilter('sku', $sku)
->setOrder('ordered_qty', 'desc')
->getFirstItem();
echo 'Quantity Ordered Today '.(int)$product->ordered_qty; ?>

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