1

I want a way of adding a coupon code that is basically just an identifier for a promotion e.g. Free Gift Wrapping or a free product give away.

I do not want to go the route of having to have the customer add a product to the basket however and thought it would just be simpler to have a 0% coupon code. This applied however no label shows at checkout or within transaction emails.

The email order items seems to use the block Magento\Sales\Block\Order\Totals to check if there is a discount with below code:

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

So i want to change this to not check if 0 but to check if a coupon code was applied instead regardless of the discount.

How can I have these coupon labels show even if there is no discount?

1 Answer 1

1

Show 0 Value Discount Coupons on Transaction Emails

I used a preference for the Magento\Sales\Block\Order\Total class to override the _initTotals() function using di.xml within a module:

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\Sales\Block\Order\Totals" type="Harrigo\EverDiscountLabel\Block\Order\Totals" />
</config>

New Method:

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

    public function _initTotals() {
        $source = $this->getSource();

        $this->_totals = [];
        $this->_totals['subtotal'] = new \Magento\Framework\DataObject(
            ['code' => 'subtotal', 'value' => $source->getSubtotal(), 'label' => __('Subtotal')]
        );

        /**
         * Add shipping
         */
        if (!$source->getIsVirtual() && ((double)$source->getShippingAmount() || $source->getShippingDescription())) {
            $this->_totals['shipping'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'shipping',
                    'field' => 'shipping_amount',
                    'value' => $this->getSource()->getShippingAmount(),
                    'label' => __('Shipping & Handling'),
                ]
            );
        }

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

        $this->_totals['grand_total'] = new \Magento\Framework\DataObject(
            [
                'code' => 'grand_total',
                'field' => 'grand_total',
                'strong' => true,
                'value' => $source->getGrandTotal(),
                'label' => __('Grand Total'),
            ]
        );

        /**
         * Base grandtotal
         */
        if ($this->getOrder()->isCurrencyDifferent()) {
            $this->_totals['base_grandtotal'] = new \Magento\Framework\DataObject(
                [
                    'code' => 'base_grandtotal',
                    'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),
                    'label' => __('Grand Total to be Charged'),
                    'is_formated' => true,
                ]
            );
        }
        return $this;
        
    }
}

This works fine by just modifying the if statement to check if a description is available allowing label to show if there is a description / label for the coupon also. I would rather do this via a plugin however this is a protected method so will have to stick to a preference.

Show 0 Value Discount Coupons on Checkout and Cart

The discount labels however still do not show for checkout or cart. Fixing this is simple enough using a mixin for Magento_SalesRule/js/view/summary/discount for checkout and Magento_SalesRule/js/view/cart/totals/discount for the cart to modify the isDisplayed() function.

view/frontend/requirejs.js

var config = {
 config: {
    mixins: {
        'Magento_SalesRule/js/view/summary/discount': {
            'Harrigo_EverDiscountLabel/js/view/summary/discount-mixin': true
        },
        'Magento_SalesRule/js/view/cart/totals/discount': {
            'Harrigo_EverDiscountLabel/js/view/summary/discount-mixin': true
        }
    }
 }
};

view/frontend/web/js/view/summary/discount-mixin.js

define(function () {
 'use strict';

 var mixin = {

    isDisplayed: function () {
            if  (this.getCouponLabel()) 
            {
                return true;
            }
            return this.isFullMode() && this.getPureValue() != 0;
    }
 };

 return function (target) {
     return target.extend(mixin);
 };
});

This will show the £0.00 in the sidebars of checkout and cart however the label is there just needs the below css to showup:

.opc-block-summary .discount.coupon, .cart-container  .discount.coupon{
    display: block;
}
2
  • I am using Magento 2.1.16. I tried the above preference. But it totally removes the Totals block from emails. However, if I edit core files. Everything works fine. But I don't want to edit core files as it is not recommended.
    – Abid Malik
    May 27, 2019 at 16:00
  • Hi, I am trying your code for "Show 0 Value Discount Coupons on Checkout and Cart". I am getting $0.00 discount but not getting discount label. As you said we have to add css to showup,could you please help where we can add above css.
    – M2 Dev
    Jun 3, 2020 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.