0

Trying to do like this:

enter image description here

I've created a Plugin, but where minicart template is placed? And how can I display the discount via knockout? Thank you.

app/code/Vendor/Checkout/Plugin/Cart.php:

<?php

namespace Vendor\Checkout\Plugin;

class Cart
{
    protected $checkoutSession;
    protected $checkoutHelper;
    protected $quote;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Checkout\Helper\Data $checkoutHelper
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->checkoutHelper = $checkoutHelper;
    }

    /**
     * Get active quote
     *
     * @return \Magento\Quote\Model\Quote
     */
    protected function getQuote()
    {
        if (null === $this->quote) {
            $this->quote = $this->checkoutSession->getQuote();
        }
        return $this->quote;
    }

    protected function getDiscountAmount()
    {
        $discountAmount = 0;
        foreach($this->getQuote()->getAllVisibleItems() as $item){
            $discountAmount += ($item->getDiscountAmount() ? $item->getDiscountAmount() : 0);
        }
        return $discountAmount;
    }

    public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result)
    {
        $result['discount_amount_no_html'] = -$this->getDiscountAmount();
        $result['discount_amount'] = $this->checkoutHelper->formatPrice(-$this->getDiscountAmount());

        return $result;
    }
}
1
  • in minicart where you want to display discount ? Aug 22, 2020 at 6:39

1 Answer 1

1

Create below files to display discount in minicart app/code/Vendor/Checkout/view/frontend/layout/checkout_cart_sidebar_total_renderers.xml

<?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">Vendor_Checkout/minicart/totals</item>
                                        </item>
                                        <item name="children" xsi:type="array">
                                            <item name="total.discount-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">Vendor_Checkout/minicart/total/discount-total</item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </item>
            </argument>
        </arguments>
    </referenceBlock>
</body>

app/code/Vendor/Checkout/view/frontend/web/template/minicart/totals.html

<!-- ko foreach: elems -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!-- /ko -->

app/code/Vendor/Checkout/view/frontend/web/template/minicart/total/discount-total.html

<div class="discount-total">
<span class="label">
    <!-- ko i18n: 'Discount Total :' --><!-- /ko -->
</span>
<div class="amount">
    <span class="price-wrapper" data-bind="html: cart().discount_amount"></span>
</div>

Please check below screen shot of output i have fetched grand-total for demo. enter image description here

6
  • Thank you so much! I receive data from my plugin but it returns 0 USD for me... Strange...
    – Aleksey
    Aug 22, 2020 at 16:29
  • What could be wrong with it?
    – Aleksey
    Aug 22, 2020 at 16:45
  • Try to add static value if it comes then there may be a something wrong with plugin data fetching Aug 22, 2020 at 18:00
  • As per your plugin code it seems that there is some extra character in function before this $this->checkoutHelper->formatPrice(-$this->getDiscountAmount()); corrected value code is $this->checkoutHelper->formatPrice($this->getDiscountAmount()); Aug 22, 2020 at 18:03
  • Thank you Harsh!
    – Aleksey
    Aug 22, 2020 at 18:40

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.