Skip to content
Snippets Groups Projects
Commit 940da84e authored by Bojan Živanović's avatar Bojan Živanović
Browse files

Issue #3047875 by bojanz: OrderAssignEvent doesn't have access to the original customer/email

parent 088d0296
Branches
Tags
No related merge requests found
......@@ -97,7 +97,7 @@ class OrderEventSubscriber implements EventSubscriberInterface {
public function onOrderAssign(OrderAssignEvent $event) {
$order = $event->getOrder();
$this->logStorage->generate($order, 'order_assigned', [
'user' => $event->getAccount()->getDisplayName(),
'user' => $event->getCustomer()->getDisplayName(),
])->save();
}
......
......@@ -21,23 +21,23 @@ class OrderAssignEvent extends Event {
protected $order;
/**
* The user account.
* The customer.
*
* @var \Drupal\user\UserInterface
*/
protected $account;
protected $customer;
/**
* Constructs a new OrderAssignEvent.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
* @param \Drupal\user\UserInterface $account
* The user account.
* @param \Drupal\user\UserInterface $customer
* The customer.
*/
public function __construct(OrderInterface $order, UserInterface $account) {
public function __construct(OrderInterface $order, UserInterface $customer) {
$this->order = $order;
$this->account = $account;
$this->customer = $customer;
}
/**
......@@ -50,14 +50,26 @@ class OrderAssignEvent extends Event {
return $this->order;
}
/**
* Gets the customer.
*
* @return \Drupal\user\UserInterface
* The customer.
*/
public function getCustomer() {
return $this->customer;
}
/**
* Gets the user account.
*
* @deprecated Use getCustomer() instead.
*
* @return \Drupal\user\UserInterface
* The user account.
*/
public function getAccount() {
return $this->account;
return $this->getCustomer();
}
}
......@@ -5,7 +5,16 @@ namespace Drupal\commerce_order\Event;
final class OrderEvents {
/**
* Name of the event fired after assigning an anonymous order to a user.
* Name of the event fired when assigning an order to a customer.
*
* Fired when assigning an anonymous order to a customer (e.g. after the
* customer logged in / registered at the end of checkout), and when
* reassigning an order to a different customer in the admin UI.
*
* Note:
* At this point the order still has the original data (customer, email).
* Use $event->getOrder()-getCustomer()->isAnonymous() to check whether the
* original customer was anonymous.
*
* Fired before the order is saved.
*
......
......@@ -41,28 +41,29 @@ class OrderAssignment implements OrderAssignmentInterface {
/**
* {@inheritdoc}
*/
public function assign(OrderInterface $order, UserInterface $account) {
$order->setCustomer($account);
$order->setEmail($account->getEmail());
public function assign(OrderInterface $order, UserInterface $customer) {
// Notify other modules before the order is modified, so that
// subscribers have access to the original data.
$event = new OrderAssignEvent($order, $customer);
$this->eventDispatcher->dispatch(OrderEvents::ORDER_ASSIGN, $event);
$order->setCustomer($customer);
$order->setEmail($customer->getEmail());
// Update the referenced billing profile.
$billing_profile = $order->getBillingProfile();
if ($billing_profile && empty($billing_profile->getOwnerId())) {
$billing_profile->setOwner($account);
$billing_profile->setOwner($customer);
$billing_profile->save();
}
// Notify other modules.
$event = new OrderAssignEvent($order, $account);
$this->eventDispatcher->dispatch(OrderEvents::ORDER_ASSIGN, $event);
$order->save();
}
/**
* {@inheritdoc}
*/
public function assignMultiple(array $orders, UserInterface $account) {
public function assignMultiple(array $orders, UserInterface $customer) {
foreach ($orders as $order) {
$this->assign($order, $account);
$this->assign($order, $customer);
}
}
......
......@@ -6,28 +6,28 @@ use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\user\UserInterface;
/**
* Handles assigning anonymous orders to user accounts.
* Handles assigning orders to customers.
*/
interface OrderAssignmentInterface {
/**
* Assigns the anonymous order to the given user account.
* Assigns the order to the given customer.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
* @param \Drupal\user\UserInterface $account
* The user account.
* @param \Drupal\user\UserInterface $customer
* The customer.
*/
public function assign(OrderInterface $order, UserInterface $account);
public function assign(OrderInterface $order, UserInterface $customer);
/**
* Assigns multiple anonymous orders to the given user account.
* Assigns multiple orders to the given customer.
*
* @param \Drupal\commerce_order\Entity\OrderInterface[] $orders
* The orders.
* @param \Drupal\user\UserInterface $account
* The user account.
* @param \Drupal\user\UserInterface $customer
* The customer.
*/
public function assignMultiple(array $orders, UserInterface $account);
public function assignMultiple(array $orders, UserInterface $customer);
}
......@@ -32,7 +32,7 @@ class OrderAssignSubscriber implements EventSubscriberInterface {
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$payment_method = $order->get('payment_method')->entity;
if ($payment_method && empty($payment_method->getOwnerId())) {
$payment_method->setOwner($event->getAccount());
$payment_method->setOwner($event->getCustomer());
$payment_method->save();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment