0

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.

0

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();
       }
    }
  • Where is the count of Customers in last 24 hrs who bought current product? – gt06 Sep 14 '17 at 4:29
0

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";
?>
  • nothing happend. I want to Paste this code in media.phml. Please help. – gt06 Sep 14 '17 at 4:38
  • @gt06, please add updated code to your media file. It will show number of times the product is purchased yesterday. – Jaimin Sutariya Sep 14 '17 at 16:12
  • nothing happend. If i purchase the item, still Purchase count is showing 0. – gt06 Sep 15 '17 at 7:26
  • @gt06, please check now. It will give you product purchased in last 24 hours – Jaimin Sutariya Sep 15 '17 at 8:07
  • nothing happend. It should also displayed that If count is 0, then displayed 0 Customers have ordered this product in Past 24 hrs, IF count > 0, count of customers have ordered this product in last 24 hrs. – gt06 Sep 15 '17 at 8:49

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.