5

I've created a small template in which I need access to information such as the currency code, cart total etc.

I'm able to get them with the methods below, but which is the most efficient? Is there a better way?

METHOD 1

$_store = Mage::app()->getStore();
$_currencyRate = $_store->getCurrentCurrencyRate();
$_currencyCode = $_store->getCurrentCurrencyCode();
$_currencySymbol = Mage::app()->getLocale()->currency($_currencyCode)->getSymbol();
$_cartTotal = Mage::getModel('checkout/session')->getQuote()->getGrandTotal();

METHOD 2

$_currencyRate = Mage::app()->getStore()->getCurrentCurrencyRate();
$_currencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$_currencySymbol = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
$_cartTotal = Mage::getModel('checkout/session')->getQuote()->getGrandTotal();

3 Answers 3

7

Before I give my answer I believe you may have a problem in your code - sessions should always be called as a singleton. Thus, the line Mage::getModel('checkout/session') should be Mage::getSingleton('checkout/session').

In my opinion, neither approach is "most correct" but the first is likely most preferable.

My approach

The matter being that there should be a separation of concerns. A template, by nature, should style data that is available to it, not fetch the data for itself. This is the very reason we have a Block class - it's a model to render the view and provide data to the template.

I would propose this:

  • Create your own custom module
  • Use your own block class
  • Provide methods to get the cart total from the block class

It would look something like this:

The block

class Company_Module_Block_Cart extends Mage_Core_Block_Template
{
    protected $_store;
    protected $_quote;

    protected function _construct()
    {
        $this->_store = Mage::app()->getStore();
        $this->_quote = Mage::getSingleton('checkout/session')->getQuote();
    }

    public function getCurrencyRate()
    {
        return $this->_store->getCurrentCurrencyRate();
    }

    public function getCurrencyCode()
    {
        return $this->_store->getCurrentCurrencyCode();
    }

    public function getCurrencySymbol()
    {
        $currencyCode = $this->getCurrencyCode();
        $localeCurrency = Mage::app()->getLocale()->currency($currencyCode);

        return $localeCurrency->getSymbol();
    }

    public function getCartTotal()
    {
        return $this->_quote->getGrandTotal();
    }
}

This allows you to discretely call each method in your template:

<h1>This is my template</h1>
<p>My currency symbol is <?php echo $this->getCurrencySymbol(); ?> and I use currency code <?php echo $this->getCurrencyCode(); ?></p>

<h2>The current cart total is: <?php echo $this->getCartTotal(); ?></h2>

I wouldn't be worried so much about DRY (don't repeat yourself) code and non-reuse of code so much as the template being clean and a frontend developer being able to create semantic markup that makes use of the data without needing to know HOW to get that data.

2
  • thanks that's a really nice answer. I guess in my module config.xml I would only need to declare the block class? Something like <global><blocks><blockmodule><class>Holy_BlockModule_Myblock</class></blockmodule></blocks><helpers>...
    – Holly
    Commented Jun 29, 2016 at 14:04
  • Correct! You would also need to refer to it in your layout.
    – philwinkle
    Commented Jun 29, 2016 at 14:07
2

The first one is probably better than the other, here's why:

  • first, it avoids redundancy of code. You don't call the exact same code several times and that makes your code easier to read, that's the biggest benefit in my opinion.
  • second, it is slightly (understand it's negligeable in this case) better in terms of performance because instead of calling Mage::app()->getStore() three times you call it once.
2

I would choose the 1st method. However, it's better to move that logic to a block to not violate the MVC design pattern.

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.