public function OrderEventSubscriber::orderUpdate in Mailchimp E-Commerce 8
Respond to event fired after updating an existing order.
File
- modules/
mailchimp_ecommerce_commerce/ src/ EventSubscriber/ OrderEventSubscriber.php, line 59
Class
- OrderEventSubscriber
- Event Subscriber for Commerce Orders.
Namespace
Drupal\mailchimp_ecommerce_commerce\EventSubscriberCode
public function orderUpdate(OrderEvent $event) {
/** @var \Drupal\commerce_order\Entity\Order $order */
$order = $event
->getOrder();
$customer = [];
$order_state = $order
->get('state')->value;
// Handle guest orders at the checkout review step - first time the user's
// email address is available.
if (empty($order
->getCustomer()
->id()) && $order
->get('checkout_step')->value == 'review') {
$customer['email_address'] = $event
->getOrder()
->getEmail();
if (!empty($customer['email_address'])) {
$billing_profile = $order
->getBillingProfile();
$customer = $this->customer_handler
->buildCustomer($customer, $billing_profile);
$this->customer_handler
->addOrUpdateCustomer($customer);
}
$order_data = $this->order_handler
->buildOrder($order, $customer);
// Add cart item price to order data.
if (!isset($order_data['currency_code'])) {
/** @var Price $price */
$price = $event
->getEntity()
->getPrice();
$order_data['currency_code'] = $price
->getCurrencyCode();
$order_data['order_total'] = $price
->getNumber();
}
$order_data['checkout_url'] = Url::fromRoute('commerce_checkout.form', [
'commerce_order' => $order
->id(),
], [
'absolute' => TRUE,
])
->toString();
$this->cart_handler
->addOrUpdateCart($order
->id(), $customer, $order_data);
}
// On order completion, replace cart in Mailchimp with order.
// TODO: Only perform action the first time an order has 'completed' status.
if ($order_state == 'completed') {
$this->cart_handler
->deleteCart($order
->id());
// Email address should always be available on checkout completion.
$customer['email_address'] = $order
->getEmail();
$billing_profile = $order
->getBillingProfile();
$customer = $this->customer_handler
->buildCustomer($customer, $billing_profile);
$order_data = $this->order_handler
->buildOrder($order, $customer);
// Update the customer's total order count and total amount spent.
$this->customer_handler
->incrementCustomerOrderTotal($customer['email_address'], $order_data['order_total']);
$this->order_handler
->addOrder($order
->id(), $customer, $order_data);
}
}