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 -
- 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
- Next, copy magento default theme "/cart/minicart.phtml" to your theme and add the KO codes.
ko text: getCartParam('subtotal_value')