Right now we have an AddressBookSubscriber that adds billing information to the address book once the order has been placed.
Shipping needs a copy of that subscriber to do the same for the shipping profile.
This is currently not a big deal cause we're talking about a 100 lines of code, but it's going to become a larger problem once "billing same as shipping" is added. So, ideally we'd have a way for shipping to say "this is my profile" and then Commerce does the rest.

Proposal:
Add an $order->collectProfiles() method. The method takes the billing profile, then fires an OrderProfiles event to collect any additional ones (such as the shipping one).

CommentFileSizeAuthor
#2 3060433-2-order-collect-profiles.patch9.57 KBbojanz

Comments

bojanz created an issue. See original summary.

bojanz’s picture

Status: Active » Needs review
StatusFileSize
new9.57 KB

Here's a proposed patch.

Notes:
1) I originally did AddressBook::collectProfiles($order) and it was fine. However, the event name is generic, and the method felt generically useful outside of the address book context. Plus, this prevents the AddressBook service from becoming even larger.

2) Had to invent $scope as a way to tag profiles. Ideally that would be the profile type, but we can't do that since we support using the same profile type for both billing and shipping.

3) Right now the CustomerProfile "instance_id" maps 1-1 to a "scope". So it might make sense to rename the instance_id setting to scope.

4) We currently have a "shipping_information" placeholder in the order and order receipt templates, filled by preprocess functions in commerce_shipping. We could use this code to actually render this ourselves.

mglaman’s picture

1) It feels weird to add yet another method to orders. I agree AddressBook::collectProfiles would bloat that service (especially with #3059633: Provide a better addressbook UI for the user pages which teeters on this same problem. What if there was an OrderProfiles service? This would allow us to collect profiles, and solve problems in 3059633. That service could set the 3rd party setting flagging a profile type for Commerce, and for loading the allowed types.

2) $scope works, I also like $intent (but it's not intent it is a "for sure" thing.) Initially, I thought of $scope and imagined a value object. But I really think that's not something worth debating. Just mentioning my initial thoughts.

3) I think having standardized naming helps.

4) 👍s/$variables['order']['billing_information']/$variables['order'][$scope . '_information'] and render those suckers. Less stuff to maintain in Shipping or custom code providing these values who may not be using shipping.

mglaman’s picture

Actually, having an OrderProfiles service would be nice. The addressbook UI patch adds

  /**
   * Get the address book profile types.
   *
   * @return \Drupal\profile\Entity\ProfileTypeInterface[]
   *   The profile types.
   */
  public function getProfileTypes();

It'd be nice to receive them keyed by scope, or get the profile type for a given scope.

  • bojanz committed 35c2f3a on 8.x-2.x
    Issue #3060433 by bojanz: Provide a way to collect all order profiles (...
bojanz’s picture

Status: Needs review » Fixed

Committed #2 + a rename of CustomerProfile's instance_id to profile_scope.

Opened a followup for #2.4: #3075246: Render each order profile automatically.

  • bojanz committed 017c0e8 on 8.x-2.x
    Issue #3060433 followup: collectProfiles() doesn't take the profiles...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.