3

If I have a customer object, what would be the best/fastest way to get a list of orders placed by that customer?

2 Answers 2

2

Try with Resource Model

$orders = Mage::getResourceModel('sales/order_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('customer_id', CUSTOMER_ID_HERE)
        //If customer is already logged in, use below line
        //->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
        ->setOrder('created_at', 'desc');

echo "<pre>";
print_r($orders->getData());
echo "</pre>";
2
  • May I ask why you prefer to use Mage::getResourceModel('sales/order_collection') instead of Mage::getModel('sales/order')->getCollection()?
    – Skytiger
    Jul 4, 2016 at 10:07
  • 1
    No big reason, but I think when you use sales/order, it calls resource model (sales/order_collection) in background, that we did directly Jul 4, 2016 at 10:26
1

You could use the following code:

$orders = Mage::getResourceModel('sales/order_collection')
          ->addAttributeToFilter('customer_email', $customer->getEmail());
2
  • May I ask why you prefer to use Mage::getResourceModel('sales/order_collection') instead of Mage::getModel('sales/order')->getCollection()?
    – Skytiger
    Jul 4, 2016 at 10:07
  • 1
    @Skytiger no particular reason it's exactly the same Jul 4, 2016 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.