4

How to get all items purchased by a customer.

Thank you for all the responses. But my issue is that I am trying to show all products in the My Downloadable Products Page. But the following code,

$_items = $this->getItems();

in the default/template/downloadable/products/list.phtml

the $_items are all downloadable products only. How can I get all products there?

3
  • using code? or else..
    – Amit Bera
    Feb 4, 2015 at 18:30
  • Using Code Please.. Feb 4, 2015 at 18:37
  • I want to get all the purchased items for the logged in customer.. Please help Feb 4, 2015 at 18:39

3 Answers 3

8

First You need to call Sales Order item collection:

$collection = Mage::getResourceModel('sales/order_item_collection')
            ->addAttributeToSelect('*');

Then put relation with sales_flat_order for getting customer email,customer id

$collection->getSelect()->join( array('orders'=> sales_flat_order),
 'orders.order_id=main_table.order_id',array('orders.customer_email','orders.customer_id'));

then get current customer id from session and filter by customer id using below code

if (Mage::getSingleton('customer/session')->isLoggedIn()) {

     $customer = Mage::getSingleton('customer/session')->getCustomer();
     $customer->getId();
    $collection->->addFieldToFilter('customer_id',$customer->getId())
  }

Then get Product Id from collection:

foreach($collection as $EachItem){

        $EachItem->getProductId();
    }

Full code:

if (Mage::getSingleton('customer/session')->isLoggedIn()) {

    $collection = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToSelect('*');
    $collection->getSelect()->join( array('orders'=> sales_flat_order),
    'orders.order_id=main_table.order_id',array('orders.customer_email','orders.customer_id'));

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

    $collection->addFieldToFilter('customer_id',$customer->getId());

    foreach($collection as $EachItem){

        $EachItem->getProductId();
    }
  }

Alternative solution:

if (Mage::getSingleton('customer/session')->isLoggedIn()) {

    $orders= Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_id',Mage::getSingleton('customer/session')->getCustomer()->getId());
    foreach($orders as $eachOrder){
        $order = Mage::getModel("sales/order")->load($eachOrder->getId()); 

        $items = $order->getAllVisibleItems();
           foreach($items as $item):
              echo $item->getProductId();
           endforeach;
       }
}
2
  • Thank you. But my issue is that I am trying to show all products in the My Downloadable Products Page. But the following code, $_items = $this->getItems(); in the default/template/downloadable/products/list.phtml the $_items are all downloadable products only. How can I get all products there? Feb 4, 2015 at 20:26
  • is there a way to get frequently ordered item for each customer?
    – Butterfly
    Oct 29, 2015 at 7:06
2

Try

//Todo : get current customer id $customerId
$items = Mage::getModel('sales/order')->getCollection()
       ->join(array('item' => 'sales/order_item'), 'main_table.entity_id = item.order_id')
       ->addFieldToFilter('customer_id', $customerId);

foreach($items as $item){
    if ($item->isDeleted() && $item->getParentItemId()) { continue; }
    echo $item->getName()
}

See How to get Magento customer ID

2
1

To get all the products of a LoggedIn Customer with order status 'Complete'

 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()))
->addFieldToFilter('status', 'complete')
->setOrder('created_at', 'desc');

$this->setOrders($orders);
foreach ($orders as $order)
{
$order_id=$order->getRealOrderId();
$order = Mage::getModel('sales/order')->load($order_id, 'increment_id');
$order->getAllVisibleItems();
$orderItems = $order->getItemsCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('product_type', array('eq'=>'simple'))
->load();
foreach($orderItems as $Item)
{
$Item = Mage::getModel('catalog/product')->setStoreId($Item->getStoreId())->load($Item->getProductId());
if ($Item->getId())
{
    echo $Item->getName();
    echo $Item->getPrice();
    echo $Item->getProductUrl();
    echo $Item->getImageUrl();
    }
}
}
?>
1
  • how can I get in magento 2 I have the only customerId. Aug 13, 2020 at 23:53

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.