Skip to content

Instantly share code, notes, and snippets.

@magevision
Last active June 10, 2019 11:54
Show Gist options
  • Save magevision/e9a7bfadbbb8a9fe93096a31fb530a71 to your computer and use it in GitHub Desktop.
Save magevision/e9a7bfadbbb8a9fe93096a31fb530a71 to your computer and use it in GitHub Desktop.
DisplayCartTotalToMiniCart
<?php
namespace MageVision\Blog45\Plugin\Checkout\CustomerData;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Checkout\Helper\Data as CheckoutHelper;
use Magento\Quote\Model\Quote;
class Cart
{
/**
* @var CheckoutSession
*/
protected $checkoutSession;
/**
* @var CheckoutHelper
*/
protected $checkoutHelper;
/**
* @var Quote|null
*/
protected $quote = null;
/**
* @param CheckoutSession $checkoutSession
* @param CheckoutHelper $checkoutHelper
*/
public function __construct(
CheckoutSession $checkoutSession,
CheckoutHelper $checkoutHelper
) {
$this->checkoutSession = $checkoutSession;
$this->checkoutHelper = $checkoutHelper;
}
/**
* Add grand total to result
*
* @param \Magento\Checkout\CustomerData\Cart $subject
* @param array $result
* @return array
*/
public function afterGetSectionData(
\Magento\Checkout\CustomerData\Cart $subject,
$result
) {
$totals = $this->getQuote()->getTotals();
$result['grand_total'] = isset($totals['grand_total'])
? $this->checkoutHelper->formatPrice($totals['grand_total']->getValue())
: 0;
return $result;
}
/**
* Get active quote
*
* @return Quote
*/
protected function getQuote()
{
if (null === $this->quote) {
$this->quote = $this->checkoutSession->getQuote();
}
return $this->quote;
}
}
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="minicart">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="minicart_content" xsi:type="array">
<item name="children" xsi:type="array">
<item name="subtotal.container" xsi:type="array">
<item name="children" xsi:type="array">
<item name="total" xsi:type="array">
<item name="component" xsi:type="string">uiComponent</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">MageVision_Blog45/minicart/totals</item>
</item>
<item name="children" xsi:type="array">
<item name="total.grand-total" xsi:type="array">
<item name="component" xsi:type="string">Magento_Checkout/js/view/checkout/minicart/subtotal/totals</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">MageVision_Blog45/minicart/total/grand-total</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</page>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\CustomerData\Cart">
<plugin name="blog45_minicart_grand_total" type="MageVision\Blog45\Plugin\Checkout\CustomerData\Cart"/>
</type>
</config>
<div class="grand-total">
<span class="label">
<!-- ko i18n: 'Cart Total :' --><!-- /ko -->
</span>
<div class="amount">
<span class="price-wrapper" data-bind="html: cart().grand_total"></span>
</div>
</div>
<!-- ko foreach: elems -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!-- /ko -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment