I am on Product detail page. Suppose I am seeing X product. Now I want to show a total number of customers who bought this X product in last 24 hrs.
Last 24 hrs should be updated every day.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI am on Product detail page. Suppose I am seeing X product. Now I want to show a total number of customers who bought this X product in last 24 hrs.
Last 24 hrs should be updated every day.
First you need to get all order collection which are purchased last 24 hours then from order collections you can easily get customer data.
To get Order Collection use below code:
$time = time();
$to_date = date('Y-m-d H:i:s', $time);
$lastTime = $time - 86400; // 60*60*24
$from_date = date('Y-m-d H:i:s', $lastTime);
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('created_at', array('from'=>$from_date, 'to'=>$to_date))
->addAttributeToFilter('status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE));
foreach($orderCollection as $order){
$order->getCustomerEmail(); //this is customer email
$items = $order->getAllVisibleItems();
foreach($items as $i){
echo $i->getProductId();
}
}
Here is the code which you can use on your product details page to get Number of customers who purchase current product.
<?php
$mySku = $_product->getSku();
$query = Mage::getResourceModel('sales/order_item_collection');
$query->getSelect()->reset(Zend_Db_Select::COLUMNS)
->columns(array('purchase_count'=>'COUNT(*)'))
->where(new Zend_Db_Expr('created_at BETWEEN NOW()-INTERVAL 1 DAY AND NOW()'))
->where('sku = ?', $mySku)
->group(array('sku'));
$purchaseCount = $query->getColumnValues('purchase_count');
$customerCount = (is_array($purchaseCount) && !empty($purchaseCount)) ? $purchaseCount[0] : 0;
echo "$customerCount Customers have ordered this product in Past 24 hrs";
?>