How to Get Order List By Customer Id Magento

In this post we will show you How to Get Order List By Customer Id Magento, hear for How to Get Order List By Customer Id Magento we will give you demo and example for implement.

Hear is code for Get Order List By Customer Id in Magento, we use this file out-side of magento(on root of magento).

<?php
// include mage file 
require_once 'app/Mage.php';
Mage::app('default');   
Mage::getSingleton("core/session", array("name" => "frontend")); 

// create functions 
function isCustomerHasOrders($customer_id, $grand_total = null)
{
	$getgetorderCollection = $this->getCustomerOrder($customer_id, $grand_total);
	return (bool)$getorderCollection->getSize();
}

function getCustomerOrder($customer_id, $grand_total = null)
{   
    $getorderCollection = Mage::getResourceModel('sales/order_collection')
        ->addFieldToSelect('*')
        ->addFieldToFilter('customer_id', $customer_id)
        ->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
        ->setOrder('created_at', 'desc');
    if ($grand_total && $grand_total > 0) 
	{
        $getorderCollection->addFieldToFilter('base_grand_total', array ('gteq' => $grand_total));
    }
    return $getorderCollection;
}
// end function 

// start code here

if (Mage::getSingleton('customer/session')->isLoggedIn()) 
{  
	$customer_info = Mage::getSingleton('customer/session')->getCustomer();
	$customer_id = $customer_info->getId();
	$order_col = getCustomerOrder($customer_id, $grand_total = null);
	$countorder = count($order_col);
	if($countorder > 0)
	{
		$i=0;
		foreach($order_col as $orderdet_val)
		{
			$order   = Mage::getModel('sales/order')->load($orderdet_val->getId());
			$orderid = $orderdet_val->getId();

			// hear get order id or order number
			$ordernumber = $order->getIncrementId();

			$orderdate1 = $order->getCreatedAtStoreDate();

			$orderdate2 = str_replace(",","",$orderdate1);
			$orderdate = trim($orderdate2,'');

			//echo date(strtotime($order->getCreatedAtStoreDate()); 

			//hear get order total value:
			$orderTotalValue = number_format ($order->getGrandTotal(), 2, '.' , $thousands_sep = '');

			$payment_method_code = $order->getPayment()->getMethodInstance()->getCode();

			$status= $order->getStatusLabel();

			$firstname = $order->getCustomerFirstname();

			$currency_code = Mage::app()->getStore()->getCurrentCurrencyCode(); 
			$lastname = $order->getCustomerLastname();

			// hear item list
			$orderItems = $order->getItemsCollection();
			$j = 0;
			foreach ($orderItems as $item)
			{
				$productid    = $item->product_id;
				$productsku   = $item->sku;
				$productprice = $item->getPrice();
				$productname  = $item->getName();
				$productqty   = $item->getData('qty_ordered');
				//echo $productsubtotal = $item->getSubTotal();
				$productsubtotal = $productqty * $productprice;

				// hear get product image 
				$productimg2 = Mage::getModel('catalog/product')->load($item->product_id);
				$productMediaConfig = Mage::getModel('catalog/product_media_config');
				$prodimgthumbnailUrl = $productMediaConfig->getMediaUrl($productimg2->getThumbnail());
				$itemdetail[$j] = array(
						"productId" => $productid,	
						"productName" => $productname,	
						"productSku" => $productsku,
						"productQty" => $productqty,
						"productPrice" => $productprice,
						"productSubTotal" => $productsubtotal,
						"prodImgUrl" => $prodimgthumbnailUrl
					);
				$j++; 
			}
			$orderlist[$i] = array(
					"orderId" => $orderid,	
					"orderNumber" => $ordernumber,
					"orderDate" => $orderdate,
					"shipTo" => $firstname.''.$lastname,
					"orderTotal" => $orderTotalValue,
					"currency" => $currency_code,
					"itemList" => $itemdetail
				);
			$i++;
		}
		$data['responseCode'] = '1';
		$data['msg'] = 'successfull';
		$data['orderListResponse'] = $orderlist;
	}
	else
	{
		$data['responseCode'] = '0';
		$data['msg'] = 'No Data Found';
	}
}
else
{
    $data['responseCode'] = '0';
    $data['msg'] = 'You must Login';
}
$result = json_encode($data);
echo $result;
?>

Hope this code and post will helped you for implement How to Get Order List By Customer Id Magento. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org