6

When customer go to checkout page default payment method should be shown checked which is Paypal. enter image description here

What is the possible way to do this. Please help guys

14

The Magento built-in payment methods don't support this ability.

But, if you have custom payment method and your custom js component extends Magento\Checkout\view\frontend\web\js\view\payment\default.js, you can use selectPaymentMethod method. In most cases, it will be enough to call it in the initObservable method:

define(['Magento_Checkout/js/view/payment/default'], function(Component) {
    initObservable: function () {
        this._super();
        this.selectPaymentMethod();
        ...
    }
});

If your payment js component has lazy initialization, you should call selectPaymentMethod on some onReady event.

| improve this answer | |
  • please add more details. this is not understanding – Divya Muralidharan Feb 18 at 9:22
11

One more example with preset default payment method as Credit Card (braintree):

app/code/Company/Checkout/view/frontend/requirejs-config.js

var config = {
    config: {
        mixins: {
            'Magento_Checkout/js/model/checkout-data-resolver': {
                'BalanceInternet_Checkout/js/model/checkout-data-resolver': true
            }
        }
    }
};

app/code/Company/Checkout/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 : 'braintree';

            if (availablePaymentMethods.length > 0) {
                availablePaymentMethods.some(function (payment) {
                    if (payment.method == paymentMethod) {
                        selectPaymentMethodAction(payment);
                    }
                });
            }
        };

        return checkoutDataResolver;
    };
});
| improve this answer | |
  • This solution worked for me. Thanks. – Shahzad H. Dec 28 '19 at 11:52
  • how to achieve the same for stored credit card ? – Hamendra Sunthwal Apr 8 at 5:06
  • I think this solution is better than accepted answer. – JanuszJanczy Jul 24 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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