Disable step submission until switching payment option ajax request is complete

Created on 8 March 2024, over 1 year ago
Updated 13 May 2024, about 1 year ago

Hello,
I just noticed this:
On the PaymentInformation pane, when the client is able to choose between multiple payment options, each time she changes her choice, an ajax request is fired because the payment_method radios has an ajax callback:

$pane_form['payment_method'] = [
      '#type' => 'radios',
      '#title' => $this->t('Payment method'),
      '#options' => $option_labels,
      '#default_value' => $default_option->getId(),
      '#ajax' => [
        'callback' => [get_class($this), 'ajaxRefresh'],
        'wrapper' => $pane_form['#id'],
      ],
      '#access' => count($options) > 1,
    ];

I noticed that if you change payment option and quickly submit the commerce step form, the new payment option that you just chose is not selected because the ajax request did not complete yet so the changes were not taken into account.
We should disable the form submit until the ajax request is complete.

This is how I managed to do this on the website I am responsible for:

(function ($, Drupal, drupalSettings) {

  'use strict';

  Drupal.behaviors.switchPaymentOption = {
    attach: function (context) {

      $(document).ajaxStart(function() {
        $("form.commerce-checkout-flow input.form-submit").prop("disabled", true);
      });

      $(document).ajaxComplete(function() {
        $("form.commerce-checkout-flow input.form-submit").prop("disabled", false);
      });

    }

  };

})(jQuery, Drupal, drupalSettings);

what do you think?

📌 Task
Status

Active

Version

2.38

Component

Checkout

Created by

🇫🇷France nicolas bouteille

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024