Question

I wish to display the number of Orders placed through the website on the Home Page, irrelevant of success or failure.

I found a similar question, but not too sure if it's the correct way to solve my problem, and also where the solution would be placed?

$processingOrders = Mage::getModel('sales/order_grid')->getCollection()
    ->addFieldToFilter('state', Mage_Sales_Model_Order::STATE_?????????);
$total = $processingOrders->count();

Will not be using the Order facility of Magento, just acting upon the emails of orders received.

I'm using Magento CE 1.9.2, and am relatively new to Magento but have extensive development and PHP / SQL experience.

Was it helpful?

Solution

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

OTHER TIPS

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.

this will give total number of order

 $processingOrders = Mage::getModel('sales/order')->getCollection();
 echo $total = $processingOrders->count();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top