Magento 1.9 – Get Ordered Items from Last 10 Days for Specific Customer

customermagento-1.9ordersproducts

I am looking for collection which have number of products that ordered by specific customer. If anyone have any suggestion please share.

Best Answer

First get all order of cutomer last 10 days.

$customer_id=10;
$toDate = date('Y-m-d H:i:s');
$fromDate = date('Y-m-d H:i:s', strtotime('-10 days',strtotime()));

$orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('customer_id', $customer_id)
    ->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));

$ordersIds =$orders->getAllIds();

Then count number of product order on that orders.

   $collection = Mage::getResourceModel('sales/order_item_collection');
   $collection->addFieldToFilter('order_id',array('in' => $ordersIds));//order_id exist in array
   $collection ->getSelect()
                ->columns('SUM(qty_ordered) as total_qty')
                ->group('product_id');
    echo $collection->getFirstItem()->getTotalQty();

    foreach($collection as $collect)
     {
        echo $collection->getTotalQty();
     }

Not tested code only giving you general idea.

Related Topic