Is there really a difference between these two code snippets?

First, getting the cart_count using getItemsCount()

    $cart_count = Mage::getSingleton('checkout/cart')->getItemsCount();

and this one getting the cart, first:

    $cart = Mage::helper('checkout/cart')->getCart();
    $cart_count = $cart->getItemsCount();

There is no difference.

Mage::helper('checkout/cart')->getCart(); and Mage::getSingleton('checkout/cart') are the same thing.

If you take a look in Mage_Checkout_Helper_Cart::getCart you will see that the only thing it does is return Mage::getSingleton('checkout/cart');.

So, I guess, if you want to be picky about it the first method is fastest by a few Plank time units but the result is the same. Second method is a wrapper for the first one.

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.