Find the Grand Total Number of Orders Placed?
$total = Mage::getModel('sales/order')->getCollection()->getSize();
You already have the code, but I'd like to give a bit more insight:
$processingOrders = Mage::getModel('sales/order_grid')->getCollection()
->addFieldToFilter('state', Mage_Sales_Model_Order::STATE_?????????);
$total = $processingOrders->count();
this will filter by state and that's not what you want:
irrelevant of success or fail.
Also count()
loads the collection and then returns the number of items, i.e. all(!) orders are loaded.
Removing the addFieldToFilter
call still leaves you with the issue, that all orders are loaded. With a few hundred thousand orders as fully loaded Magento models this will significantly hurt performance, if not crash PHP.
And then there's getSize()
. In contrast to count()
it transforms the SQL query that would load the collection into a SELECT COUNT(*)
query. That's a single fast query that returns exacltly one number, nothing more.
This is why @Marius answer is the correct way:
Mage::getModel('sales/order')->getCollection()->getSize();
How to add it to the home page
To add the number in the CMS, you can create a new PHTML file in your theme with just this content:
<?php echo Mage::getModel('sales/order')->getCollection()->getSize();
Save it as app/design/frontend/[package]/[theme]/template/page/ordercount.phtml
.
Then use the following code in your CMS:
{{block type="core/template" name="ordercount" template="page/ordercount.phtml" cache_lifetime="600"}}
I added a cache lifetime of 10 minutes (600 seconds) because you should not trigger the query on each request, even if it's optimized now. Adjust the number to your needs.