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
- Create registration.php file at app\code\Meetanshi\Fixpaymentrule directory
123456<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'Meetanshi_Fixpaymentrule',__DIR__); - Create module.xml at app\code\Meetanshi\Fixpaymentrule\etc directory
1234567<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> - Create routes.xml at app\code\Meetanshi\Fixpaymentrule\etc\frontend directory
1234567<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> - Create ApplyPaymentMethod.php at app\code\Meetanshi\Fixpaymentrule\Controller\Checkout directory
12345678910111213141516171819202122232425262728293031323334353637383940<?phpnamespace 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();}} - Create requirejs-config.js at app\code\Meetanshi\Fixpaymentrule\view\frontend directory
12345678var config = {map: {'*': {'Magento_Checkout/js/action/select-payment-method':'Meetanshi_Fixpaymentrule/js/action/select-payment-method'}}}; - Create select-payment-method.js at app\code\Meetanshi\Fixpaymentrule\view\frontend\web\js\action directory
123456789101112131415161718192021222324252627define(['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.
Hope it helps! If you face any doubts in the method, feel free to mention them in the Comments section below and I’d be happy to help.
Thank you.
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 businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
18 Comments
Thansks for publishing this.
It almost works. the order is getting discount, but when redirected to gateway, it seems that the rule is removed.
Im using swissup firecheckout, so it might be a compatibility issue? but again, the total looks right, just until one selects “place order” then it’s removed.
Hello Jesper,
Please disable firecheckout and check, it might be the issue of firecheckout
Thank You
HI Sanjay Jethva
I’m getting issue on checkout when I apply coupon code on checkout cart or checkout shipping page at the end of checkout payment page I’m getting issue with my billing address same as shipping getting only 2 things once pin code or second country name
Hello Jitendra,
Please enable log in ApplyPaymentMethod.php file to check if you are getting proper quote data, billing, and shipping address.
If you are not getting the proper address from the quote, call shippingSaveProcessor.saveShippingInformation(quote.shippingAddress().getType()); before ajax request using ‘Magento_Checkout/js/model/shipping-save-processor’ in the select-payment-method.js file.
Thank you.
Hi the module is working fine but where can i set the discount amount
Hello,
If you have create rule for discount which is not displayed in the cart page, please refer the solution at – https://meetanshi.com/blog/add-magento-shopping-cart-price-rules/
Thank you.
hi mine didn’t work magento 2.3.4, i saw in your answer that it possibly would be. It looks like Magento_Checkout / js / action / select-payment-method from another extension should be canceled.
Log in and check which JS console is coming.
I actually have an extension on the payment page that calls its own JS file. How do I solve this? for the two work operations together?
Hello,
You need to merge the code of your JS file and my JS file for the solution to work.
Thank you.
Thanks . is it necessary any setting to show discount for cash on delivery?
Hey,
You do not need any other settings.
If you have created the discount rule, it will work fine.
Thank you.
is it working for cash on delivery? its urgent
Hello,
Yes, it works for all the payment methods including Cash on Delivery.
Thank you.
sanjay , i have done the above changes, but two things.
No call on deeloper console when payment method changes
and moreover in cart rules , payment method option is not avialble?
Hello Sharjeel,
It seems that the Magento_Checkout/js/action/select-payment-method of another extension must be overrided.
Please log in and check to find in which console the JS is coming.
Thanks.
Hi Sanjay, the module worked very well in Magento 2.3.4.
Hey, thanks.
Happy to help.
I have enabled the module, but its not showing the calculated discounts on checkout
Hello,
Please check the console when you change the payment method if ajax is called or not.
Thanks.