In Magento 1.7, I need to get all products that each customer bought. I already know how to obtain them but the problem is that the code returns orders, but not separated by customers. I have the following:

//Start Magento application
    //Get all ordered items
    $col = Mage::getModel('sales/order_item')->getCollection();    
    foreach($col as $item)
        echo $item->getOrder()->getCustomerName().' bought '. $item->getProduct()->getSku()."\n";

So, I am obtaining orders and from there I see which is the customer that ordered it. But I wonder if there is some method that allow me to, first, obtain the list of all customers, and then for each customer, obtain all the orders and then all products inside the order. For obtaining customers I have:

$customers = Mage::getModel('customer/customer')->getCollection();
    foreach($customers as $customer)

And I tried $order = $customer->getOrders(); because in recent.phtml it is used and works fine, but here getOrders() is returning null.

I would like to know if there is a simple way to accomplish my purpose. Any help will be appreciated.


2 Answers 2


You can use below code snippet to get List of bought products for each customer.

$collection = Mage::getModel("sales/order")->getCollection();
              'skus' => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ", ")')
foreach ($collection as $data){
    echo $data->getCustomerName()." bought " . $data->getSkus()."<br />";

It will show result like,

Mark Woodland bought abl001, hde013, abl001, hde013, abl001, hde013
Haven Bangor bought hdd001, hdd002, hdd001, hdd002
  • Works perfectly and even a bit more faster than what I had. Do you see any reason for that? Maybe some piece of the code you include is more efficient? (I never saw this Zend_Db_Expr for example)
    – LeCodeur
    Commented Jul 20, 2017 at 6:30
  • 1
    @LeCodeur, Its because we are using less database resources and less looping with above code. We can get all required data with only a single query with join. Zend_Db_Expr is Magento's library function to prepare text for database query. the main thing is group_concat(sales_flat_order_item.sku SEPARATOR ", ") which gathers all skus in a single column. Commented Jul 20, 2017 at 6:35
  • Thank you for taking your time to explain, that was useful.
    – LeCodeur
    Commented Jul 20, 2017 at 6:37

You can get by using following code

//Get All Users/Customers
$users = mage::getModel('customer/customer')->getCollection()

foreach ($users as $user){
    $customer_id = $user['entity_id'];

    //Get the  all orders by customer id
    $order_collection = Mage::getResourceModel('sales/order_collection')
        ->setOrder('created_at', 'desc'); 

        foreach($order_collection as $orders) {
            echo $increment_id = $orders['increment_id']."</br>";
            $order_id = $orders['entity_id'];

            //Get all items for the order id
            $order = Mage::getModel('sales/order')->load($order_id);

            $items = $order->getAllItems();

            foreach($items as $i) {

                  echo $i->getName()."</br>";

            echo  "</br>";

  • 1
    Thank you, works fine, I just accepted the other answer because the result is closer to what I expected.
    – LeCodeur
    Commented Jul 20, 2017 at 6:28
  • glad to help you
    – Mujahidh
    Commented Jul 20, 2017 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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