Pages

Thursday 15 January 2015

Magento:: Show Product Sold Quantity On Detail Page

1- If you want to show stock sold on the product page for the entire duration the product has been active, use the following code:
<?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; ?>
2- if you want to show Qty ordered today, use the following code:
<?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; ?>
3- 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, $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; ?>

No comments:

Post a Comment