Magento获取设定日期之间的订单总和 [英] Magento Get SUM of order totals between set dates

查看:91
本文介绍了Magento获取设定日期之间的订单总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用常规的mySQL来做到这一点,但我希望能够像以前一样以"magento方式"做到这一点...

I can do this with regular mySQL but I would like to be able to do it "the magento way" as it were...

我想做的是运行一个查询,该查询将对设定日期之间的订单总计进行SUM(grand_total),即计算出2012年7月的总收入.

What I would like to do, is run a query which will SUM(grand_total) for my order totals between set dates, ie work out the total revenue from July 2012.

我对此进行了各种尝试,我可能真的很近,或者我可能相距一百万英里,所以我感谢任何人都能给我的帮助!到目前为止,我有:

I've tried various variations on this, and I might be really close or I might be a million miles away, so I'd appreciate any help anyone can give me! What I have so far is:

$orders = Mage::getModel('sales/order')->getCollection();

$orders->addAttributeToFilter('date_field', array(
'from' => '2011-09-01',
'to' => '2011-09-30',
));
$orders->addExpressionAttributeToSelect('grand_total', 'SUM({{grand_total}})', grand_total);
$orders_total->getSelect()->$orders->grand_total(SUM(grand_total));

提前谢谢!

推荐答案

宏伟的方式" 将使用您的问题列出了自7月以来的所有订单?如果是这种情况,那么您只需要在过滤器中输入"from",而不需要"to" ...

Your question states all orders since July? If this is the case then you only require the 'from' in the filter and not the 'to'...

$orderTotals = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('status', Mage_Sales_Model_Order::STATE_COMPLETE)
    ->addAttributeToFilter('created_at', array('from'  => '2012-07-01'))
    ->addAttributeToSelect('grand_total')
    ->getColumnValues('grand_total')
;
$totalSum = array_sum($orderTotals);

// If you need the value formatted as a price...
$totalSum = Mage::helper('core')->currency($totalSum, true, false);

这篇关于Magento获取设定日期之间的订单总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆