Magento: Get all orders totals by day

If you find with many orders, you tend to hit memory limits pretty quickly, so I only load a year or so at a time. If you run into memory limits, you might need to do the same. Ignore all that if you like. I also didn’t test this because I don’t have my test installation handy (it’s just a quick alteration to some code I already had laying around), but this should be a decent start to aggregating the data, at least.

$totals = array();

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

foreach ($ocol->getItems() as $order) { 
    $createdat = new DateTime($order->getCreatedAt());
    $totals[$datetime->format('w')] += $order->getBaseGrandTotal();

You should now have an array where the key is a date, and the value is the total for that date. It uses all orders though, regardless of status. Not sure if that’s what you’re looking for or not, but since you haven’t mentioned what you’ve tried so far, I’m guessing that you’re just looking for a starting place, and this might be enough for that. Feel free to ask if something doesn’t make sense.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s