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

2 Answers 2

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.

2
  • please add more details. this is not understanding Commented Feb 18, 2020 at 9:22
  • This solution will rewrite the selected payment method. End-users can't change the payment method anymore.
    – HoangHieu
    Commented Oct 14, 2020 at 12:40
20

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;
    };
});
5
  • This solution worked for me. Thanks. Commented Dec 28, 2019 at 11:52
  • 1
    how to achieve the same for stored credit card ? Commented Apr 8, 2020 at 5:06
  • 2
    I think this solution is better than accepted answer. Commented Jul 24, 2020 at 13:46
  • I have tried this and also the module techflarestudio.com/… But when i try to default select payment method, place order button is disabled and not able to order. When we check again the already showing selected payment method again then the place order is active. Please suggest any solution. Commented Jun 13, 2022 at 7:32
  • The solution worked for me as well thanks!! Commented Jun 16, 2022 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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