OrderEventSubscriber.php in Mailchimp E-Commerce 8
File
modules/mailchimp_ecommerce_commerce/src/EventSubscriber/OrderEventSubscriber.php
View source
<?php
namespace Drupal\mailchimp_ecommerce_commerce\EventSubscriber;
use Drupal\commerce_order\Event\OrderAssignEvent;
use Drupal\commerce_order\Event\OrderEvent;
use Drupal\commerce_order\Event\OrderEvents;
use Drupal\Core\Url;
use Drupal\mailchimp_ecommerce\CartHandler;
use Drupal\mailchimp_ecommerce\CustomerHandler;
use Drupal\mailchimp_ecommerce\OrderHandler;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderEventSubscriber implements EventSubscriberInterface {
private $order_handler;
private $cart_handler;
private $customer_handler;
public function __construct(OrderHandler $order_handler, CartHandler $cart_handler, CustomerHandler $customer_handler) {
$this->order_handler = $order_handler;
$this->cart_handler = $cart_handler;
$this->customer_handler = $customer_handler;
}
public function orderUpdate(OrderEvent $event) {
$order = $event
->getOrder();
$customer = [];
$order_state = $order
->get('state')->value;
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);
if (!isset($order_data['currency_code'])) {
$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);
}
if ($order_state == 'completed') {
$this->cart_handler
->deleteCart($order
->id());
$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);
$this->customer_handler
->incrementCustomerOrderTotal($customer['email_address'], $order_data['order_total']);
$this->order_handler
->addOrder($order
->id(), $customer, $order_data);
}
}
public function orderAssign(OrderAssignEvent $event) {
$order = $event
->getOrder();
$account = $event
->getAccount();
$customer['email_address'] = $account
->getEmail();
$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);
if (!isset($order_data['currency_code'])) {
$price = $event
->getOrder()
->getTotalPrice();
if ($price) {
$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);
}
public static function getSubscribedEvents() {
$events[OrderEvents::ORDER_UPDATE][] = [
'orderUpdate',
];
$events[OrderEvents::ORDER_ASSIGN][] = [
'orderAssign',
];
return $events;
}
}