Wednesday 20 August 2014

get all customers filter by top bestshaler and full order detail by customer in magento


first get all customer id and total sale:-

<?php $customer_ids=array();
$grand_total= array();
$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToSelect('customer_id')
    ->addAttributeToSelect('base_grand_total')
    ->addAttributeToSort('base_grand_total', 'DESC');
foreach ($orders as $order) {

    $ids=$order->getCustomerId();
    if(!empty($ids)){
    array_push($customer_ids,$ids);//push customer id in a array
   
    $gd_total+=$order->getBaseGrandTotal();
   
    array_push($grand_total,$gd_total);//push customer sale in a array
   
     }
   
}

sum total sale according to id

$result=array();
foreach($customer_ids as $k=>$key)
 {
   foreach($grand_total as $vk=>$val)
    {
      if($vk==$k)
       {
      if(!array_key_exists($key, $result))
       {
          $result[$key]=$val;
       }
       else{
        $sum_key=$result[$key];
        $result[$key]=($sum_key + $val);//sum same id value
       }

       }
    }

 }
 arsort($result);//sale value sorting sorting.


?>
$result array contain all id and total sale of customer.
get all detail of cutomer by id.
<table>
 <tr>
 <td width="50">ID</td> <td width="150">Name </td><td width="250">Email</td><td width="100">Total Purchase</td><td width="100">Detail</td></tr>
 <?php
 foreach($result as $ids =>$pur)
 {
 ?>
 <tr>

 <?php
$customer_data = Mage::getModel('customer/customer')->load($ids);
?>
<td><?php echo $customer_data['entity_id']; ?></td>
<td><?php echo $customer_data['firstname']." "; ?><?php echo $customer_data['lastname']; ?></td>
<td><?php echo $customer_data['email']; ?></td>
<td><?php echo $pur; ?></td>
 <td><a href="?id=<?php echo $customer_data['entity_id']; ?>">View</a></td>
 </tr>
 <?php
 }
?>
</table>

to get full detail of customer by customer id :-

<?php
$get_id=$_GET['id'];
 if(!empty($get_id))
{?>
<div id="detail" style="width:700px;background-color:#9933CC; margin-left:150px;">
<table style="margin-left:100px;">
 <tr>
 <td width="50">ID</td> <td width="150">Name </td><td width="250">Email</td></tr>
<tr>
 <?php //get full detail of customer by id
$customer_data = Mage::getModel('customer/customer')->load($get_id);
?>
<td ><?php echo $customer_data['entity_id']; ?></td>
<td><?php echo $customer_data['firstname']." "; ?><?php echo $customer_data['lastname']; ?></td>
<td><?php echo $customer_data['email']; ?></td>

</td>
 </tr>
 </table>
 </div>
 <div style="width:700px;background-color:#999933; margin-left:150px;">
 <table>
 <?php
  //get order id by customer id
 $orderCollection = Mage::getModel('sales/order')->getCollection()
 ->addFieldToFilter('customer_id',$get_id);
 foreach($orderCollection as $ord)
 {

 array_push($ord_id,$ord->getId());//push order id in a array
 }
// print_r($ord_id);
 ?>
 <tr>       
 <td width="150">Order NO.</td>
<td width="150">Product Id</td>
<td width="150">Product Sku</td>
<td width="150">Product Quantity</td>
<td width="150">Product Name</td>
<td width="150">Product Price</td>
<td width="150">Date</td>
</tr>
 <?php

  foreach($ord_id as $id_set){ 
 
 $orders = Mage::getModel("sales/order")->load($id_set);
   $ordered_items = $orders->getAllItems();
   foreach($ordered_items as $item){ 
        ?>
<tr>
<td ><?php echo $orders->getIncrementId(); ?></td>/*get order number*/       
<td ><?php echo $item->getItemId(); ?></td>
<td ><?php echo $item->getSku(); ?></td>
<td ><?php echo $item->getQtyOrdered(); ?></td>
<td ><?php echo $item->getName(); ?></td>
<td><?php echo $item->getPrice(); ?></td>
<td ><?php echo $item->getCreatedAt(); ?></td>
</tr>
<?php
         }
    }   
 ?>
 </table>
 </div>
</div>
<?php } ?>

No comments:

Post a Comment