Checkout "save to my address book" saves the addresses to the anonymous user instead of the current user

Created on 28 December 2020, almost 4 years ago
Updated 5 June 2023, over 1 year ago

Problem/Motivation

In the checkout page, if you try to use "save to my address book" checkbox, the current edited address will be saved to the anonymous user instead of the current user. Those addresses are then impossible to delete from the administrative interface, forcing you to remove them directly from the database.

Steps to reproduce

In a clean Drupal 8.9.x or 9.0.x core with commerce 2.21.

Set up a default store with a default checkout. Enable and set up the commerce owns example payment gateway module, to have access to the billing form.

When you attempt to save the address into the address book using the "add to address book" checkbox, the behaviour described above happens.

Proposed resolution

A workaround until the issue is fixed in the commerce module itself. Using hook_form_alter you can add your own submit method in the "next" form button, and save manually the profile to the right user, following this commerce profile documentation.

// Create a new, active profile for a user.
$user = user_load_by_mail('customer@example.com');
$profile = \Drupal\profile\Entity\Profile::create([
'type' => 'customer',
'uid' => $user->id(),
'status' => TRUE,
'address' => [
'country_code' => 'US',
'postal_code' => '53177',
'locality' => 'Milwaukee',
'address_line1' => 'Pabst Blue Ribbon Dr',
'administrative_area' => 'WI',
'given_name' => 'Frederick',
'family_name' => 'Pabst',
],
]);
$profile->save();

💬 Support request
Status

Closed: works as designed

Version

2.21

Component

Checkout

Created by

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024