0

I have to make the total of the same customer who has many orders. means i need the sum of all orders of 1 customer I tried this.

$cus = Mage::getModel('sales/order')->getCollection();
foreach($cus as $customer)
{
echo "<pre>";
$i=Mage::getModel('sales/order')->load($customer->getEntityId());
echo "<pre>";
$j=($i->getstatus());
if($j == complete)
{
    print_r($i->getbaseGrandTotal());
    //print_r($i->getData());
    print_r($i->getCustomerEmail());        
}
else
{
    echo "bye";     
}}
  • What happened when you tried that? What is the problem? Looks like you're looping through the orders, verifying their status, and then accessing the grand total- why not just total them? Also, it would help you get help if you indented your code and used more complete (descriptive) variable names. – Mikel Bitson Feb 22 '17 at 18:08
  • actually, i put a condition that if order status is complete than come to if condition & in if condition I have to put the overall total of 1 customer who made multiple order so please help me @MikelBitson – Jaimin Feb 22 '17 at 18:22
3

Use below code:

$orders = Mage::getModel('sales/order')->getCollection()
     ->addAttributeToFilter('customer_id', 'customerId here');

foreach ($orders as $order) {
    $total = $order->getGrandTotal();
     $sum+= $total;

}
   echo $sum ;

If you want order filter by order status use below code:

$orders = Mage::getModel('sales/order')->getCollection()
     ->addAttributeToFilter('customer_id', 'customerId here')
     ->addFieldToFilter('status', 'complete');

 foreach ($orders as $order) {
        $total = $order->getGrandTotal();
         $sum+= $total;

    }
       echo $sum ;

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.