Swapnil Bhavsar
Swapnil Bhavsar

Reputation: 683

How to get order list for logged in customer in magento

I am working on getting list of order numbers(name) ordered by a customer. I have tried using

Mage::getModel('sales/order')->load($order_id);

but didn't works for me? Actually i am working on help desk module and trying to assign orders to the tickets.

Upvotes: 3

Views: 13840

Answers (4)

Hardik
Hardik

Reputation: 1283

You can try this as well,

$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();     

Now you have customer email address in your $email variable. So you can easily get order collection using this email address as following:

 $orderCollection = Mage::getModel(‘sales/order’)->getCollection();
 $orderCollection->addFieldToFilter(‘customer_email’, $email);

foreach ($orderCollection as $_order)
{
    echo $_order->getRealOrderId() ;
    echo $this->formatDate($_order->getCreatedAtStoreDate()) ;
    echo $_order->getShippingAddress() ? $this->escapeHtml($_order->getShippingAddress()->getName()) ;
    echo $_order->formatPrice($_order->getGrandTotal());
    echo $_order->getStatusLabel();
 }

Upvotes: 3

Swapnil Bhavsar
Swapnil Bhavsar

Reputation: 683

Ok friends thanks for your hints, I got this by using this

require_once 'app/Mage.php'; 
      Mage::app();       

      $orders = Mage::getResourceModel('sales/order_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
        ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
        ->setOrder('created_at', 'desc')
    ;   

     $this->setOrders($orders); 

     foreach ($orders as $order):

    echo $order->getRealOrderId().' at '.$this->formatDate($order->getCreatedAtStoreDate()).' ('.$order->formatPrice($order->getGrandTotal()).')';

    endforeach;

Upvotes: 12

Josua M C
Josua M C

Reputation: 3158

you can try this:

$yourCustomerId = '123123';
$field          = 'customer_id';
$collection     = Mage::getModel("sales/order")->getCollection()
                   ->addAttributeToSelect('*')
                   ->addFieldToFilter($field, $yourCustomerId);
echo "<pre>";
print_r($collection);
echo "</pre>";

// if the customer is logged in you can add this
if(!Mage::getSingleton('customer/session')->isLoggedIn()){
    $yourCustomerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
    $field          = 'customer_id';
    $collection     = Mage::getModel("sales/order")->getCollection()
                       ->addAttributeToSelect('*')
                       ->addFieldToFilter($field, $yourCustomerId);
    echo "<pre>";
    print_r($collection);
    echo "</pre>";
}

Upvotes: 3

Roatha Chann
Roatha Chann

Reputation: 445

$orders = Mage::getModel('sales/order')->getCollection();

$orders->getSelect()->where('e.customer_id ='.$customer_id);

Upvotes: 1

Related Questions