How to Pre-select Default Payment Method at the Checkout Page in Magento 2

 


var config = {

    config: {

        mixins: {

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

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

            }

        }

    }

};

Step 2: After that, we must create a “checkout-data-resolver.js” file inside the extension at the following path.


app\code\Vendor\Extension\view\frontend\web\js\model


Now add the code as given below


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 : 'cashondelivery';

                //set payment method as per your requirement(i.e cashondelivery replace with your payment method) 

 

            if (availablePaymentMethods.length > 0) {

                availablePaymentMethods.some(function (payment) {

                    if (payment.method == paymentMethod) {

                        selectPaymentMethodAction(payment);

                    }

                });

            }

        };

        return checkoutDataResolver;

    };

});

Comments

Popular posts from this blog

Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid in Magento 2

How to Update Product Stock Programmatically in Magento 2

How to Set and Get Cookie in Magento 2