Generated with the help of an LLM.
Problem/Motivation
modules/wstack/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php sets either the stored address book profile itself (selected address) or a profile it just created for the customer (new address) as the order's billing profile:
// new address: $profile = $this->saveBillingAddress(...) (created with 'uid' => $customer->id())
// stored address: $profile = $profile_storage->load($billing_address['address_selection']);
// Do a copy from the profile.
\Drupal::service('commerce_order.address_book')->copy($profile, $this->order->getCustomer());
// Add profile to order (commerce change this to uid 0).
$this->order->setBillingProfile($profile);
Order::preSave() in Commerce core makes the billing profile owned by the order ($billing_profile->setOwnerId(0)), so that profile leaves the address book. Judging by the comments, the code expects AddressBook::copy() to leave a copy in the address book.
That only happens when copy() creates a duplicate: with multiple: true (the Commerce default) and no address_book_profile_id on the profile. With multiple: false, copy() first looks up the customer's address book profile (AddressBook::load() → ProfileStorage::loadByUser(), default profile first) and updates that one instead of creating a duplicate. If the customer has no other profile, that is the very profile just selected or created: it is updated in place (its address_book_profile_id then points to itself) and the order takes it over. If an older default profile exists, that one is updated and the new profile still leaves the address book (last case below).
I replayed the pane's original sequence against Commerce 3.3.14 and Profile 1.17.0, one scenario per process, each rolled back. Results for profile.type.customer (address book before → after):
multiple: true(default), stored address:[P]→[duplicate of P](address kept, new profile id)multiple: true(default), new address, book empty:[]→[copy](kept)multiple: false, stored address:[P]→[](lost)multiple: false, new address, book empty:[]→[](not saved)multiple: false, new address, book has a default profile P:[P]→[P](P updated with the new address, the new profile still leaves the book)
So the code works with the default configuration, but not on sites that allow only one address per customer.
Steps to reproduce
- Set the
customerprofile type to "Allow multiple profiles per user: No" (profile.type.customer,multiple: false). Have a customer with one saved address. - Check out with the WStack gateway, keep the stored address selected, continue to the review step.
- The customer's address book is empty. The profile now has uid 0, and its
datacontainsaddress_book_profile_idpointing to its own id. A customer without a saved address who enters a new one also ends with an empty address book.
Proposed resolution
Give the order its own profile (uid 0) that references the address book entry through address_book_profile_id, as Commerce core's checkout does (it creates an order-owned profile and fills it with populateFromProfile()):
- Stored address:
$profile = $address_book_profile->createDuplicate(),setOwnerId(0),setData('address_book_profile_id', <address book profile id>), save. Do not callcopy(). - New address: create the profile owned by the order (uid 0) in
saveBillingAddress()and callAddressBook::copy()with it. That creates or updates the address book entry for both settings ofmultiple.
Patch attached. Replaying the patched sequence against Commerce 3.3.14, the address stays in the address book in all cases above, for both settings of multiple (stored address: book unchanged; new address: entry created, updated or added). With multiple: false it was also checked in real checkouts through the Wallee sandbox.
| Comment | File | Size | Author |
|---|---|---|---|
| commerce_wallee-wstack-address-book-copy.patch | 2.4 KB | piridium |
Comments
Comment #3
cola commentedThanks for the detailed analysis and the patch! I traced it against AddressBook::copy() and Order::preSave() and can confirm the behavior with multiple: false.
Committed to 8.x-3.x with a few small additions on top of your patch:
- guard against saveBillingAddress() returning FALSE and against a selected profile that can't be loaded (logged instead of a fatal error)
- the order-owned duplicate no longer inherits is_default.
One known limitation remains: every submission of the pane creates a new order-owned billing profile instead of reusing the order's existing one, as Commerce core does. I'll handle that in a follow-up.
Comment #5
piridium commentedThanks! Good catch with the additions!