我正试图提取第一季度的销售总额。我似乎无法让它发挥作用。有什么建议吗?
$total = 0;
$orders = Mage::getModel('sales_order/collection')
->addAttributeToSelect('*')
->addAttributeToFilter('created_at', array(
'from' => '2012-01-01',
'to' => '2012-03-31'));
foreach ($orders as $order) {
$total += $order->getGrandTotal();
}
echo $total;
答案 0 :(得分:1)
您没有正确收集该集合。有多种方法可以做到这一点,看起来你可能会结合几种方法:
Mage::getModel('sales/order')->getCollection()
或Mage::getResourceModel('sales/order_collection')
但是,如果您真正想要的是将单个属性grand_total
相加,则方式更有效地构建您自己的查询,而不是加载整个销售订单集合:
$db = Mage::getSingleton('core/resource')->getConnection('core_read');
$salesTable = Mage::getSingleton('core/resource')->getTableName('sales/order');
list($total) = $db->fetchCol(
$db->select()
->from($salesTable, array('grand_total' => 'SUM(grand_total)'))
->where('DATE(created_at) >= ?', '2012-01-01')
->where('DATE(created_at) <= ?', '2012-03-31')
);
echo $total;