over 8 years ago
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.
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);
$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:
In this way we can load list of orders of the current logged in user.
0 Comment(s)