Magento Freezing on Currency Change

I’ve recently been looking at introducing multi-currency options on a Magento website (so the customer can see the price in pounds, dollars, euros etc). The problem I’ve been finding was that the website would freeze after I made a new choice of currency, and eventually the web server time out the request.

Today I had a chance to see what’s going on…

By adding mysql database logging I could see magento was making repeated SQL statements like this…

SELECT `sales_flat_quote`.* FROM `sales_flat_quote` WHERE (`sales_flat_quote`.`entity_id`='321') AND (store_id IN ('1')) AND (is_active = 1)

In fact there were over 8K requests to the sales_flat* tables in a very short space of time!

Eventually with a bit of googling I narrowed down the problem to a rogue bit of code in a custom module used for quantity based discounts across multiple different colour/size variations of the same configurable product.

The line of code causing the problem was:
if($items = Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection())

This caused an infinite loop where the getting of the items in the collection caused it to get the prices again, which in turn fired this piece of code off once again, and so on…

Changing the code to:

$quoteId = Mage::getSingleton('checkout/session')->getQuoteId();
$quote = Mage::getModel('sales/quote')->load($quoteId);
if($items = $quote->getItemsCollection())

has solved the problem.