I have a site that primarily sells one type of product in several quantities (50, 100, 200 and 400). How would I go about dynamically showing on the homepage the total amount of these products ordered over the lifetime of the site?

For example, if I had ten total orders of the product in quantity of 50 and five of the product in quantity of 200, how do I have the homepage dynamically count and display the total of 2500 [(5 x 50) + (10 x 200)]?

Also, it might not be a good idea to have this calculated every time the homepage loads - so is there any way to have the calculating script run every half hour or so (cron?)?

Is there perhaps an extension that does this?

I am using Magento 1.8.1


What you don't want to do:

Don't implement (or take any advice to implement) a count based on the sales/order collection. Why? Any operation happening on that table has the potential to cause a lock and will have direct impact live production orders (this is why they use a table sales_flat_order_grid for the admin view).

What you can do:

There are dozens of ways to do this. All of them involve querying the database and updating a record somewhere. It depends on how real-time this needs to be. I'm guessing doing this every day or so is probably sufficient.

You could, for instance:

  • Create a Magento Custom Variable that stores the value and then echo it via a static block on the homepage
  • Create a Magento Store Config value and access it from just about anywhere with Mage::getStoreConfig('my_stores/awesome/sales_number_is_so_high);

Some (including me) may consider that this is an abuse of store config... and you'd probably be right. But maybe you want that number to appear higher some days? This allows you to override the number from the admin. It also has the benefit of being cached.

In all cases, though, you'll be updating it on a schedule. I highly recommend you read up on how Magento CRON works.

How you tackle this problem really has a lot to do with how much load you are under; if it is significant I would possibly make a custom table with a value that can be cached by both Magento (at the block level or even with cache tag) and potentially MySQL at the db layer. Regardless, you shouldn't be updating this very often as to incur invalidated caches.

Speaking of cache, you're on Community so you don't have EE Full Page Cache to have out in front of this functionality... so best be on the safe side and get it into something like Config XML that can be cached by default in an in-memory cache (APC/memcached/redis).

In terms of potential load, if you do have a very high sales volume that is, most sites eschew a realtime/ajax loader for a faked javascript counter that averages orders/day or hour.

Just show me the code

Getting the order count is simple:

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

If you decide save it into the config:

 $orderCount = Mage::getModel('sales/order')->getCollection()->getSize();
 Mage::getModel('core/config')->saveConfig('sales/order/count', $orderCount );

Now to get it back out:

echo Mage::getStoreConfig('sales/order/count');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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