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
    • 420
    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.

     

    $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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: