You are here

public function OrderHandler::buildOrder in Mailchimp E-Commerce 8

@inheritdoc

Overrides OrderHandlerInterface::buildOrder

File

src/OrderHandler.php, line 91

Class

OrderHandler
Order handler.

Namespace

Drupal\mailchimp_ecommerce

Code

public function buildOrder(Order $order, array $customer) {
  $order_items = $order
    ->getItems();
  $lines = [];

  /** @var OrderItem $order_item */
  foreach ($order_items as $order_item) {
    $line = [
      'id' => $order_item
        ->id(),
      'product_id' => $order_item
        ->getPurchasedEntity()
        ->getProductId(),
      // TODO: Figure out how to differentiate between product and variant ID here.
      'product_variant_id' => $order_item
        ->getPurchasedEntityId(),
      'quantity' => (int) $order_item
        ->getQuantity(),
      'price' => $order_item
        ->getUnitPrice()
        ->getNumber(),
    ];
    $lines[] = $line;
  }
  $order_data = [
    'customer' => $customer,
    'processed_at_foreign' => date('c'),
    'lines' => $lines,
  ];
  if (isset($customer['address'])) {
    $order_data['billing_address'] = $customer['address'];
  }
  if (!empty($order
    ->getTotalPrice())) {
    $order_data['currency_code'] = $order
      ->getTotalPrice()
      ->getCurrencyCode();
    $order_data['order_total'] = $order
      ->getTotalPrice()
      ->getNumber();
  }
  return $order_data;
}