Billing profile owner id is set to 0 on every order save

Created on 8 March 2021, over 3 years ago
Updated 21 January 2023, almost 2 years ago

Currently, on its preSave hook, Orders reset the billing profile's owner id to 0 for whatever reason.

If you're creating an Order programmatically, along its components (OrderItems, customer's Profile, etc), and you follow the documentation on this, you set the profile's owner to the corresponding user id, then, when you save the order, the profile's owner is reset to 0, loosing the link.

This is problematic for various reasons, you might be using the profile elsewhere in the application, as a user's address book, as shipping profile, or any other use case. Additionally, why does the Order resets the profile's owner just for the billing profile and not for any other profile types the order might have, such as shipping, etc? Seems very arbitrary, as the Order does have a method for retrieving all profiles, but reset's the ownership of the billing profile only.

IMHO, the Order shouldnt mess with external entities such as a Profile in its preSave hook or in this way.

In our current project, the customer has an address book with his/her different profiles (shipping, billing, etc.) ala Amazon, they can set default or preferred profiles, etc. We had to scratch our heads why we were unable to retrieve the user's profiles anymore, until we found this.

💬 Support request
Status

Fixed

Version

2.24

Component

Order

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.

  • 🇻🇳Vietnam cp19112000

    I suggest using a different flag to handle custom logic instead of using copy_to_address_book (s.t like owner_presave_event_dispatched)
    You can set owner_presave_event_dispatched to TRUE before adding a profile and then patch into the code "Make sure the billing profile is owned by the order, not the customer"
    In my case, I customized some events in ProfileOwnerEvents to ensure the duplicated profile keeps owner_presave_event_dispatched TRUE and the owner is the customer. I don't want to use copy_to_address_book because the duplicated profile set copy_to_address_book to FALSE and set address_book_profile_id to the old profile, which made relationships between two profiles

          if ($billing_profile->getData('owner_presave_event_dispatched')) {
            $event = new ProfileOwnerEvent($billing_profile);
            $event_dispatcher = \Drupal::service('event_dispatcher');
            $event_dispatcher->dispatch($event, ProfileOwnerEvents::PROFILE_OWNER_PRESAVE);
            $billing_profile = $event->getProfile();
            $this->setData('owner_presave_event_dispatched', FALSE);
          }
    
Production build 0.71.5 2024