1

I'm trying to generate the list of past ordered products for logged in customer. My attempt so far.

<ul>
<?php 
    $OrderProducts = 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');       

                foreach($OrderProducts as $ord){            

                    $o=Mage::getModel('sales/order')->load($ord->getId());  
                    $items = $o->getAllItems(); ?>


                    <?php foreach($items as $p){
                          $p= Mage::getModel('catalog/product')->load($p->getId());
                          ?>                         
                        <li>
                         <a href="<?php echo $p->getProductUrl()?>"><img class="recently-img" src="<?php echo $this->helper('catalog/image')->init($p, 'small_image')->keepFrame(false) ->keepAspectRatio(true)->resize(140) ?>" /></a>
                       </li>        

          <?php }} ?>
</ul>

This code showing some past order items but some contents are wrong. And also it does not show those products which are configurable products. Please help. Thanks in advance.

2

I found a much more performance friendly version.

By joining the two tables order and order_items together, Magento returns a collection with all purchased items.

$items = Mage::getModel('sales/order')->getCollection()
    ->join(array('item' => 'sales/order_item'), 'main_table.entity_id = item.order_id')
    ->addFieldToFilter('customer_id', $customerId);
  • this only works as long as only one item has been ordered :-( Help is appreciated! – Denis Nov 27 '14 at 14:15
1
if (Mage::getSingleton('customer/session')->isLoggedIn()) {

/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();

}



$collection = Mage::getModel('sales/order')
                    ->getCollection()
                      ->addAttributeToFilter('customer_email',array('like'=>$customer_email));

foreach($collection as $order){
    //do something
    $order_id = $order->getId();



$order = Mage::getModel("sales/order")->load($order_id); //load order by order id 

$ordered_items = $order->getAllItems(); 

foreach($ordered_items as $item){     //item detail     

echo $item->getItemId(); //product id     

echo $item->getSku();    
 echo $item->getQtyOrdered(); //ordered qty of item    

 echo $item->getName();   

 } 
}
  • But this code or my previous code does not show configurable ordered products. Can you help me to show configurable purchased products also? – Poles Jul 25 '14 at 9:07
1
<?php
    require_once("app/Mage.php");
    $app = Mage::app('');

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

    /* Get the customer data */
    $customer = Mage::getSingleton('customer/session')->getCustomer();
    /* Get the customer's email address */
    $customer_email = $customer->getEmail();

    }



    $collection = Mage::getModel('sales/order')
                    ->getCollection()
                      ->addAttributeToFilter('customer_email',array('like'=>$customer_email));

    foreach($collection as $order){
    //do something
    //echo '<pre>';print_r($order);
   echo $order->increment_id; //order id
   echo $order->created_at; // Order data
   echo $order->status; // Order status
   echo $order->grand_total; // total amount

?>

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.