Solution: Discount Based on Payment Method Not Showing in Magento 2 Cart Total

Offering discounts may seem to be a concern for the profit of the business. But, implementing it effectively leads to brand recognization, attract new customers and win customer loyalty.

“Employed thoughtfully, discounts can give sales a healthy boost; handled incorrectly, they can devalue and detract from the bottom line.”

Talking about the discounts, it is important that customers acknowledge that they are offered the discount! However, for Magento 2 stores, there is sometimes an issue of discount based on payment method not showing in Magento 2 cart total . To address this issue, one can make a payment method visible only to admin and offer the discount through that payment method, ensuring that only the admin can apply the discount to the customer’s order. This way, the discount will be displayed in the cart total for the customer to see, and they can benefit from the discount while still maintaining a smooth checkout experience. So if you are offering multiple payment option and you want to redirect your customers to one particular payment method encourage them by giving Magento 2 discount on payment methods.

If the order summary does not reflect the discount calculation in the cart, how are you ever going to leverage the benefits of discounts?

The discount is applied but does not show in the cart. It may lead to the confusion for the customer as to whether he benefitted from using a particular payment method or not? Such confusions result in an abandoned cart. Surely, we don’t want that and hence implement the solution given below!

Also, you can download package of the below solution directly from GitHub.

Solution: Discount Based on Payment Method Not Showing in Magento 2 Cart Total

1. Create registration.php file at app\code\Meetanshi\Fixpaymentrule directory

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Meetanshi_Fixpaymentrule',
    __DIR__
);

2. Create module.xml  at app\code\Meetanshi\Fixpaymentrule\etc directory

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/Module/etc/module.xsd">
    <module name="Meetanshi_Fixpaymentrule" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Rule"/>
        </sequence>
    </module>
</config>

3. Create routes.xml  at app\code\Meetanshi\Fixpaymentrule\etc\frontend directory

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="Meetanshi_Fixpaymentrule" frontName="fixpaymentrule">
            <module name="Meetanshi_Fixpaymentrule"/>
        </route>
    </router>
</config>

4. Create ApplyPaymentMethod.php at  app\code\Meetanshi\Fixpaymentrule\Controller\Checkout directory

<?php

namespace Meetanshi\Fixpaymentrule\Controller\Checkout;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\ForwardFactory;
use Magento\Framework\View\LayoutFactory;
use Magento\Checkout\Model\Cart;
use Magento\Framework\App\Action\Action;

class ApplyPaymentMethod extends Action
{
    protected $resultForwardFactory;
    protected $layoutFactory;
    protected $cart;

    public function __construct(
        Context $context,
        ForwardFactory $resultForwardFactory,
        LayoutFactory $layoutFactory,
        Cart $cart
    )
    {
        $this->resultForwardFactory = $resultForwardFactory;
        $this->layoutFactory = $layoutFactory;
        $this->cart = $cart;

        parent::__construct($context);
    }

    public function execute()
    {
        $pMethod = $this->getRequest()->getParam('payment_method');
        $quote = $this->cart->getQuote();
        $quote->getPayment()->setMethod($pMethod['method']);
        $quote->setTotalsCollectedFlag(false);
        $quote->collectTotals();
        $quote->save();
    }
}

5. Create requirejs-config.js  at  app\code\Meetanshi\Fixpaymentrule\view\frontend directory

var config = {
    map: {
        '*': {
            'Magento_Checkout/js/action/select-payment-method':
                'Meetanshi_Fixpaymentrule/js/action/select-payment-method'
        }
    }
};

6. Create select-payment-method.js at  app\code\Meetanshi\Fixpaymentrule\view\frontend\web\js\action directory

define(
    [
        'Magento_Checkout/js/model/quote',
        'Magento_Checkout/js/model/full-screen-loader',
        'jquery',
        'Magento_Checkout/js/action/get-totals',
        'mage/url',
    ],
    function (quote, fullScreenLoader, jQuery, getTotalsAction,url) {
        'use strict';
        return function (paymentMethod) {
            quote.paymentMethod(paymentMethod);

            fullScreenLoader.startLoader();

            var linkUrl = url.build('fixpaymentrule/checkout/applyPaymentMethod');
            jQuery.ajax(linkUrl, {
                data: {payment_method: paymentMethod},
                complete: function () {
                    getTotalsAction([]);
                    fullScreenLoader.stopLoader();
                }
            });

        }
    }
);

That’s it.

With the above solution, if you offer a payment method based discount, it will always show the calculations clearly in the cart total, leaving no room for any confusions! If you can also solve Magento 2 navigation menu not showing error, as top navigation menu makes it easy for visitors to navigate through the Magento 2 store.

Thank you.

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...