0

Need to add functionality to Set Stored credit card as default payment method in payment options checkout page magento 2 https://prnt.sc/rvdou9

3 Answers 3

0

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.

5
  • Could you please provide a bit more detail ? i want to set store credit card as selected payment option prnt.sc/rve6lh Commented Apr 8, 2020 at 12:23
  • 1
    you want the card to be as a selected which payment method are you using Commented Apr 8, 2020 at 12:26
  • Stripe stored card credit card , payment method "stripe" Commented Apr 8, 2020 at 12:27
  • there will be one js which will be rendering the payment method there you can use some trick but before you need to debug that from where it's showing Commented Apr 8, 2020 at 12:28
  • I've set the store card payment method code and also set the credit card on the frontend using jquery onchange but then place order button does't work , nothing happens after clicking place order button , but once i toggle the payment option ans select the same card then it works. Commented Apr 8, 2020 at 12:31
0

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;
    };
});
1
0

Achieved this functionality using cookie. Set cookie and get its value and set it on payment method option.

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.