I'm trying to pull out the sales totals for quarter 1. I can't seem to get this to work. Any suggestions?
$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;
You're not getting the collection properly. There are multiple ways to do this, looks like you might've combined a couple methods:
Mage::getModel('sales/order')->getCollection()
or Mage::getResourceModel('sales/order_collection')
However, if all you really want is to sum the single attribute grand_total
it is way more efficient to build your own query rather that loading the entire sales order collection:
$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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.