0

I am in Product Detail Page. I want to show how many customers already had bought this product.

$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 86400; // 60*60*24
$from = date('Y-m-d H:i:s', $lastTime);
$items = Mage::getResourceModel('sales/order_item_collection')
   ->addAttributeToSelect('order_id')
   ->addAttributeToSelect('created_at')
   ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to));
$soldCount = 0;
foreach ($items as $item) {
   $soldCount += $item->getQtyOrdered() - $item->getQtyCanceled() - $item->getQtyRefunded();
}
<?php if (count($soldCount) > 0):?>
  <div class="order-enticer button--l animated fadeInDown">Good Choice... 
       <b><?php echo $soldCount;?></b> 
       people have ordered this in the past 24 hours.</div>
<?php endif; ?>
0

Try this code in your product view.phtml-

$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 86400; // 60*60*24
$from = date('Y-m-d H:i:s', $lastTime);
$items = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToSelect('order_id')
    ->addAttributeToSelect('item_id')
    ->addAttributeToSelect('created_at')
    ->addAttributeToFilter('item_id', $_product->getId())
    ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to)); //filter with date to get only last 24 hours order


$soldCount = 0;
foreach ($items as $item) {
    $soldCount += $item->getQtyOrdered() - $item->getQtyCanceled() - $item->getQtyRefunded();
}
if($soldCount > 0){
    echo $soldCount.' people have purchased this product';
} else {
    echo 'Be the first to buy this product!';
}
10
  • Also i want to show, only total customers in past 24 hrs. – gt06 Sep 9 '17 at 14:05
  • @gt06 please check my updated answer – Piyush Sep 9 '17 at 14:09
  • not working...no error is displaying. A product is properly displaying. – gt06 Sep 10 '17 at 13:09
  • can you update your question with code ? – Piyush Sep 11 '17 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.