-1

Coupon code not showing when discount amount is 0.00

enter image description here

enter image description here

0

Try overriding below js in your theme/module

vendor/magento/module-sales-rule/view/frontend/web/js/view/cart/totals/discount.js

In this file there is below code

isDisplayed: function () {
    return this.getPureValue() != 0; //eslint-disable-line eqeqeq
}

Try replacing it with

isDisplayed: function () {
    return this.getCouponCode() != null; //eslint-disable-line eqeqeq
}

I have not tested the code but you can give it a try.

Update

For checkout page summary try overriding same method in below file

vendor/magento/module-sales-rule/view/frontend/web/js/view/summary/discount.js

Your final method should be as below-

isDisplayed: function () {
    return this.isFullMode() && this.getCouponCode() != null; //eslint-disable-line eqeqeq
},
| improve this answer | |
  • Thank you for answer your code is working in cart page fine right but i need in checkout order summery (prnt.sc/qov49o) – Rakesh Donga Jan 17 at 4:55
  • i have implement on this file but not working for me i have try that before when you have answer and may here here return two functions prnt.sc/qov9af – Rakesh Donga Jan 17 at 5:14
  • does it work when you remove the isFullMode condition and add the coupon code condition? – Piyush Jan 17 at 5:23
  • i have try that but not working .... – Rakesh Donga Jan 17 at 5:25
  • it should work, please check if file is not override by any other module Or you have override file properly – Piyush Jan 17 at 5:26
0

app/code/VENDOR/MODULE/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\SalesRule\Model\Quote\Discount" type="VENDOR\MODULE\Model\Quote\Discount" />
</config>

app/code/VENDOR/MODULE/Model/Quote/Discount.php

<?php

namespace VENDOR\MODULE\Model\Quote;

class Discount extends \Magento\SalesRule\Model\Quote\Discount
{
    public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
    {
        $result = null;
        $amount = $total->getDiscountAmount();

        $description = $total->getDiscountDescription();
        $result = [
            'code' => $this->getCode(),
            'title' => strlen($description) ? __('Discount (%1)', $description) : __('Discount'),
            'value' => $amount
        ];

        return $result;
    }
}

you need to override module-sales-rule/view/frontend/web/template/cart/totals/discount.html into your theme. and add below code into it

<tr class="totals">
    <th colspan="1" style="" class="mark" scope="row">
        <span class="title" data-bind="text: getTitle()"></span>
        <span class="discount coupon" data-bind="text: getCouponLabel()"></span>
    </th>
    <td class="amount" data-bind="attr: {'data-th': title}">
        <span><span class="price" data-bind="text: getValue()"></span></span>
    </td>
</tr>

i removed below lines from discount.html file

<!-- ko if: isDisplayed() --> and <!-- /ko -->

do same with this file also module-sales-rule/view/frontend/web/template/summary/discount.html for checkout page

you will get your result :D

| improve this answer | |
  • i have tested this code in magento2.3.3, it is working as per your need, you don't need to change anything – Hiren Jan 17 at 6:44
  • you can ignore if(1) condition, updated the answer – Hiren Jan 17 at 6:47
  • i need only checkout summery page nothing cart page – Rakesh Donga Jan 17 at 6:47
  • still you need to override Discount class, and removed isDisplayed from module-sales-rule/view/frontend/web/template/summary/discount.html – Hiren Jan 17 at 6:49
  • Let us continue this discussion in chat. – Rakesh Donga Jan 17 at 6:51
0

Edit: I am sorry this is for the admin part. I red the question too fast and didn't realise it was for the frontend. But i will leave the answer here since i think the same logic could apply for the frontend and that you might also want it in the backend.

In vendor/magento/module-sales/Block/Adminhtml/Totals.php in the function _initTotals you have some code like this :

    if ((double)$this->getSource()->getDiscountAmount() != 0) {
        if ($this->getSource()->getDiscountDescription()) {
            $discountLabel = __('Discount (%1)', $this->getSource()->getDiscountDescription());
        } else {
            $discountLabel = __('Discount');
        }
        $this->_totals['discount'] = new \Magento\Framework\DataObject(
            [
                'code' => 'discount',
                'value' => $this->getSource()->getDiscountAmount(),
                'base_value' => $this->getSource()->getBaseDiscountAmount(),
                'label' => $discountLabel,
            ]
        );
    }

You have to change the condition for

if ((double)$this->getSource()->getDiscountAmount() != 0)

To

if ($this->getSource()->getDiscountAmount() != null)

Now since you should not change this directly in the core you can create an override on the class \Magento\Sales\Block\Adminhtml\Order\Totals like that :

<?php
namespace Vendor\ModuleName\Block\Adminhtml\Order;

class Totals extends \Magento\Sales\Block\Adminhtml\Order\Totals
{

    protected function _initTotals()
    {
        parent::_initTotals();

        /**
         * Add discount
         */
        if ($this->getSource()->getDiscountAmount() !== null) {
            if ($this->getSource()->getDiscountDescription()) {
                $discountLabel = __('Discount (%1)', $this->getSource()->getDiscountDescription());
            } else {
                $discountLabel = __('Discount');
            }
            $this->_totals['discount'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'discount',
                    'value' => $this->getSource()->getDiscountAmount(),
                    'base_value' => $this->getSource()->getBaseDiscountAmount(),
                    'label' => $discountLabel,
                ]
            );
        }


        return $this;
    }
}

Edit 2 : For the front end you will have to do the exact same thing but you will have to override the file vendor/magento/module-sales/Block/Order/Totals.php instead of vendor/magento/module-sales/Block/Adminhtml/Totals.php.

| improve this answer | |
  • I edited my answer, to override the frontend you have to do the same override but on a different file. – Carlo Ricci Oct 17 '19 at 20:49
  • i want to for frontend and i have use your code as per your suggestion but not working for me – Rakesh Donga Jan 16 at 5: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.