6

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.

3 Answers 3

4
$total = Mage::getModel('sales/order')->getCollection()->getSize();
2
  • Excellent @Marius, thank you so much. Can this code be inserted in the Home page inside CMS or does the associated .phtml file have to be edited?
    – Barton
    Commented Dec 17, 2015 at 17:42
  • you can use it in a model, or a helper, or a block or a phtml.
    – Marius
    Commented Dec 17, 2015 at 17:59
2

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.

1

this will give total number of order

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.