Sometimes in magento we are required to get list of orders of the current logged users.
To do so lets see how we can do it:
first of all we need to get the logged in user id from the session for the same write the below code to fetch id of the user.
$user_id = Mage::getSingleton('customer/session')->getCustomer()->getId();
Afterwards we will write a query to load list of the orders of the user with this id.
Lets write the query and pass the user id to it :
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', $user_id)
->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
->setOrder('created_at', 'desc');
$this->setOrders($orders);
In the above code we are passing the user id and from the resource model 'sales/order_collection' we are loading the the order with the customer id as
$user_id in descending or to get the latest order on the top.
Now lets print the order id in foreach loop:
$i=0
foreach ($orders as $_order){
echo $i.'Order Id: '.$_order->getRealOrderId();
$i++;
}
In this way we can load list of orders of the current logged in user.
0 Comment(s)