Magento: Grab/Display Customer's Lifetime Sales Stats and Total Amount of Orders on Order View Pages
In the Order View (the main screen when viewing a customer's order), I would like to add a few table rows and add the Customer's Lifetime Sales (Total Sales) and Total Amount of Orders.
The file I want to add the code is here: app/design/adminhtml/default/default/template/sales/order/view/info.phtml
In this file, below:
<?php foreach ($this->getCustomerAccountData() as $data):?>
<tr>
<td class="label"><label><?php echo $data['label'] ?></label></td>
<td class="value"><strong><?php echo $data['value'] ?></strong></td>
</tr>
<?php endforeach;?>
开发者_开发百科
Is where I'd add my the rows/cells I need with the correct information. Could you please tell me how to get the necessary data?
this is how you get the lifetime sales:
$customer = Mage::getModel('customer/customer')->load($customerId);
$customerTotals = Mage::getResourceModel('sales/sale_collection')
->setOrderStateFilter(Mage_Sales_Model_Order::STATE_CANCELED, true)
->setCustomerFilter($customer)
->load()
->getTotals();
$customerLifetimeSales = $customerTotals->getLifetime();
$customerNumberOfOrders = $customerTotals->getNumOrders();
This is a var_dump of $customerTotals:
object(Varien_Object)[662]
protected '_data' =>
array
'lifetime' => float 10503.13
'base_lifetime' => float 10503.13
'base_avgsale' => float 92.9480530973
'num_orders' => int 113
'avgsale' => float 92.9480530973
protected '_hasDataChanges' => boolean false
protected '_origData' => null
protected '_idFieldName' => null
protected '_isDeleted' => boolean false
You can place that code in a method on your block and call it in each row. Somthing like:
public function getCustomerTotals(Mage_Customer_Model_Customer $customer)
{
// TODO: Add necessary validation...
return Mage::getResourceModel('sales/sale_collection')
->setOrderStateFilter(Mage_Sales_Model_Order::STATE_CANCELED, true)
->setCustomerFilter($customer)
->load()
->getTotals();
}
and in your code there you can add:
<?php $customerTotals = $this->getCustomerTotals($customer); ?>
<td><?php echo $customerTotals->getLifetime(); ?></td>
Don't know if it's due to an upgrade, but now it's necessary to add this:
->setOrderStateFilter(Mage_Sales_Model_Order::STATE_CANCELED, true)
So, the code will be:
return Mage::getResourceModel('sales/sale_collection')
->setOrderStateFilter(Mage_Sales_Model_Order::STATE_CANCELED, true)
->setCustomerFilter($customer)
->load()
->getTotals();
精彩评论