Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get list of all orders of the logged in user?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 421
    Comment on it

    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.

     

    1. $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 :

    1. $orders = Mage::getResourceModel('sales/order_collection')
    2.  
    3. ->addFieldToSelect('*')
    4.  
    5. ->addFieldToFilter('customer_id', $user_id)
    6.  
    7. ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
    8.  
    9. ->setOrder('created_at', 'desc');
    10.  
    11. $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:


     

    1. $i=0
    2.  
    3. foreach ($orders as $_order){
    4.  
    5. echo $i.'Order Id: '.$_order->getRealOrderId();
    6.  
    7. $i++;
    8.  
    9. }
    10.  

    In this way we can load list of orders of the current logged in user.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: