"My billing information is the same as my shipping information." checkbox state wrong / inconsistent with AJAX

Created on 2 October 2024, about 2 months ago

Problem/Motivation

Yesterday we invested several hours in Allow control over "billing same as shipping" default Needs review try to implement a setting for the "My billing information is the same as my shipping information." checkbox.

The reason, why we did that was, that it was always unchecked by default in our case.

We had PayPal (without separate billing / shipping addresses) as first payment method and Bank transfer as second one (with separate billing / shipping).

The thing we didn't see early enough, is this nasty bug affecting the checkbox state of the corresponding checkbox in checkout:

    $inline_form['copy_fields']['enable'] = [
      '#type' => 'checkbox',
      '#title' => $this->getCopyLabel($inline_form),
      '#default_value' => $enabled,
      '#ajax' => [
        'callback' => [get_class($this), 'ajaxRefresh'],
        'wrapper' => $inline_form['#id'],
      ],
    ];

(src/ProfileFieldCopy.php)

The checkbox works totally fine and as expected, representing the corrent $enabled default, if not loaded through AJAX. That's the case, if the affected payment method is the 1st one and loaded with the page, without AJAX.

If the payment method is loaded through AJAX (>1 payment methods), the checkbox should still have $enabled checked, but hasn't and is loaded unchecked!

This is tricky to find, and of course the behaviour should be consistent for end-users which are otherwise affected by unexpected / bad for the ajax-loaded payment methods.

I'm unsure about the root cause and wasn't able to fix it.
Possible causes:

Steps to reproduce

See above

Proposed resolution

Fix the bug so that the checkbox state is represented correctly through AJAX and initial load.

Remaining tasks

  1. Investigate the root cause
  2. Implement a test with >1 payment method and AJAX switching of the payment method
  3. Fix the issue
  4. Release fix and test

User interface changes

None

API changes

None

Data model changes

None

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

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

Comments & Activities

  • Issue created by @Anybody
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica

    Commenting out this part

    if ($user_input) {
          if (isset($user_input['copy_fields'])) {
            $enabled = (bool) ($user_input['copy_fields']['enable'] ?? FALSE);
          }
          elseif (isset($user_input['select_address'])) {
            $enabled = FALSE;
          }
        }
    

    or setting

    $enabled = TRUE;
    

    by hard doesn't even change the situation. On first ajax load, the checkbox is always unchecked!

Production build 0.71.5 2024