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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 464
    Comment on it

    In magento if we need to get the list of products of orders of the current logged in customer. 
    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 customer id. 
    Lets write the query and pass the customer id to it :

    $orders = Mage::getResourceModel('sales/order_collection')
               ->addFieldToSelect('*')
               ->addFieldToFilter('customer_id', $customer_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 orders with the customer id as $customer_id in descending order to get the latest order on the top.

    Now we need to load the list of all products from the orders for that purpose we are required to work on a foreach loop as :

    foreach ($orders as $_order){
    
     $order = Mage::getModel('sales/order')->load($_order->getId());
      $order->getAllVisibleItems();
      $orderItems = $_order->getItemsCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('product_type', array('eq'=>'simple'))
        ->load();
    
     foreach($orderItems as $sItem){
      echo 'Product Name: '.$sItem->getName().'<br>';
      echo 'Product Price: '.$sItem->getPrice().'<br>';  
     }
    
    }

    In the above code we loaded the order from the model 'sales/order' with the order id as $_order->getId();
    Afterwards we loaded the items of the order of type simple then in the other nested foreach loop we printed products and their prices one by one.
    In this way we will get the products list from the orders of the logged in customer.

 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: