1

I have added the following to minicart.phtml for my theme and it doesn't work.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart'); 

$subTotal = $cart->getQuote()->getSubtotal();
$grandTotal = $cart->getQuote()->getGrandTotal();
Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69

2 Answers2

4

This above code will works on the page load, but will not work with magento2 ajax add to cart as it uses Knockout JS now.

For that you should use -

  1. Override the magento class "\Magento\Checkout\CustomerData\Cart" in your module and extend method "getSectionData"
    public function getSectionData()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of Object Manager
        $priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data'); // Instance of Pricing Helper

        $totals = $this->getQuote()->getTotals();
        return [
            'summary_count' => $this->getSummaryCount(),
            'subtotal' => isset($totals['subtotal'])
                ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
                : 0,
            'subtotal_value' => isset($totals['subtotal'])
                ? $priceHelper->currency($totals['subtotal']->getValue(),true,false)
                : '',
            'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
            'items' => $this->getRecentItems(),
            'extra_actions' => $this->layout->createBlock('Magento\Catalog\Block\ShortcutButtons')->toHtml(),
            'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
        ];
    }

Here I have added a new cart param "subtotal_value" as the "subtotal" will return the price container span and it will display as TEXT using KO. Here you have to use "Object Manager Instance" directly, as you wont be able to inject dependencies to the "__construct".

NOTE, there are few exception where we might need to use "Object Manager Instance" directly. In our case it is backward compatibility of constructor.
ObjectManager Exception

  1. Next, copy magento default theme "/cart/minicart.phtml" to your theme and add the KO codes.
ko text: getCartParam('subtotal_value')
Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Sandipan S
  • 929
  • 8
  • 11
  • Thanks. This definitely assisted me. However, subtotal if null doesn't display anything still. Any work around for that? – user5762246 Sep 28 '16 at 21:40
  • yes, in that case you can use the KO codes to show 0 or any text. – Sandipan S Oct 03 '16 at 10:41
  • Please vote it up if it helps you..!! – Sandipan S Oct 03 '16 at 10:49
  • @user5762246 .. please mark this as the accepted answer if it really solves your issue. – Sandipan S Nov 24 '16 at 13:00
  • @SandipanS thank you for the solution. Also note that that the knockout code (for example 'ko text: getCartParam('subtotal_value')'). Should be inside the 'data-bind="scope: 'minicart_content'"' div, else it doesn't work. – Tjitse Apr 19 '17 at 09:37
2

for grand total

<?php    $helper = $this->helper('\Magento\Checkout\Helper\Cart');
         echo $this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format($helper->getQuote()->getGrandTotal(),2),true,false); ?>

sub total

<?php    $helper = $this->helper('\Magento\Checkout\Helper\Cart');
         echo $this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format($helper->getQuote()->getSubtotal(),2),true,false); ?>
Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
  • What's the proper way of utilizing that besides putting it in minicart.phtml file. I thought you had to utilize ko for dynamic stuff? Also, how would you get it to display 0.00 if carts empty? – user5762246 Sep 27 '16 at 19:01
  • the number format will do the trick for you display 0.00. for KO i didn't touch that yet – Qaisar Satti Sep 28 '16 at 07:34