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?

  • using code? or else.. – Amit Bera Feb 4 '15 at 18:30
  • Using Code Please.. – Magento_Newbie Feb 4 '15 at 18:37
  • I want to get all the purchased items for the logged in customer.. Please help – Magento_Newbie Feb 4 '15 at 18:39
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;
       }
}
|improve this answer|||||
  • 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? – Magento_Newbie Feb 4 '15 at 20:26
  • is there a way to get frequently ordered item for each customer? – Butterfly Oct 29 '15 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

|improve this answer|||||
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();
    }
}
}
?>
|improve this answer|||||

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.