How To Pre Select Default Payment Method In Magento 2

Hello Everyone,

In this blog, we will learn about how to Pre Select Default Payment Method in the Checkout Page in Magento 2.

Checkout is a One Important Step for customers. So that if the store owner pre-selects the checkout method that customers most select, it’s easy for customers for a seamless shopping journey. 

Without wasting your time, let us guide you straight away. Follow the easy step given below to Pre Select Default Payment Method in Magento 2.

STEPS FOR PRE SELECT DEFAULT PAYMENT METHOD IN MAGENTO 2

Step 1: Create requirejs-config.js file

app/code/Vendor/Extension/view/frontend/requirejs-config.js

var config = {

    config: {

        mixins: {

            'Magento_Checkout/js/model/checkout-data-resolver': {

                'Vendor_Extension/js/model/checkout-data-resolver': true

            }

        }

    }

};

Step 2: Create checkout-data-resolver.js file

app/code/Vendor/Extension/view/frontend/web/js/model/checkout-data-resolver.js

define([

    'Magento_Checkout/js/model/payment-service',

    'Magento_Checkout/js/checkout-data',

    'Magento_Checkout/js/action/select-payment-method'

], function(

    paymentService,

    checkoutData,

    selectPaymentMethodAction

) {

    'use strict';

    return function(checkoutDataResolver) {

        checkoutDataResolver.resolvePaymentMethod = function() {

            var availablePaymentMethods = paymentService.getAvailablePaymentMethods(),

                selectedPaymentMethod = checkoutData.getSelectedPaymentMethod(),

                paymentMethod = selectedPaymentMethod ? selectedPaymentMethod : 'banktransfer';

                //set payment method as per your requirement 

            if (availablePaymentMethods.length > 0) {

                availablePaymentMethods.some(function (payment) {

                    if (payment.method == paymentMethod) {

                        selectPaymentMethodAction(payment);

                    }

                });

            }

        };

        return checkoutDataResolver;

    };

});

Step 3: Finally run the below commands

$ php bin/magento setup:upgrade

$ php bin/magento cache:clean

$ php bin/magento cache:flush

Step 4: Output:


Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Pre Select Default Payment Method in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *