Thomas Nielsen
Thomas Nielsen

Reputation: 613

Get magento subtotal from cart

I'm currently using this snippet to show the cart totals in the topcart of my Magento shop. My problem is that it's not always updating when products is put in cart, it's just showing 0$, especially configurable products. But when a second product is put in the cart, it's working again.

Am I missing something, should there be a "check" of some kind before this piece of code?

<?php echo Mage::helper('checkout')->formatPrice($this->getSubtotal()) ?>

Upvotes: 7

Views: 23925

Answers (4)

Head
Head

Reputation: 568

None of the above worked for me but I was able to get the subtotal using this:

$orderObj = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$orderSubTotal = $orderObj->getSubtotal();
echo $orderSubTotal;

This refers to the success.phtml page.

Upvotes: 1

Shadow
Shadow

Reputation: 103

you can use this code:

$subtotals= Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();

echo $formattedPrice = Mage::helper('core')->currency($subtotals , true, false);

Upvotes: 2

Swapna Taru
Swapna Taru

Reputation: 688

You can also try following code it works for me

<?php echo Mage::helper('checkout/cart')->getQuote()->getSubtotal() ?>

Upvotes: 17

Drew Hunter
Drew Hunter

Reputation: 10114

Make sure your top cart block is extending a relevant block type such as Mage_Checkout_Block_Cart_Sidebar. If you do, you will have access to useful functionality that will save you rewriting unnecessary code.

For example, if you extend Mage_Checkout_Block_Cart_Sidebar - you can call getSubtotal()

An alternative would be to use the following:

Mage::getSingleton('checkout/session')->getQuote()->getSubtotal();

Upvotes: 2

Related Questions