You are here

public function CustomerHandler::buildCustomer in Mailchimp E-Commerce 8

@inheritdoc

Overrides CustomerHandlerInterface::buildCustomer

File

src/CustomerHandler.php, line 142

Class

CustomerHandler
Customer handler.

Namespace

Drupal\mailchimp_ecommerce

Code

public function buildCustomer($customer, $billing_profile) {
  $customer_id = 0;
  $list_id = mailchimp_ecommerce_get_list_id();
  $customer_id = $this
    ->loadCustomerId($customer['email_address']);
  if (!empty($customer_id)) {
    $customer['id'] = $customer_id;
  }

  // Pull member information to get member status.
  $memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
  $opt_in_status = isset($memberinfo->status) && $memberinfo->status == 'subscribed' ? TRUE : FALSE;
  $customer['opt_in_status'] = $opt_in_status;
  if ($billing_profile && $billing_profile->address) {
    $address = $billing_profile->address
      ->first();
    $customer['company'] = $address
      ->getOrganization();
    $customer['first_name'] = $address
      ->getGivenName();
    $customer['last_name'] = $address
      ->getFamilyName();
    $customer['orders_count'] = (int) $this
      ->getCustomerTotalOrders($customer['email_address']);
    $customer['total_spent'] = $this
      ->getCustomerTotalSpent($customer['email_address']);
    foreach ($customer as $key => $value) {
      if ($value === NULL) {
        unset($customer[$key]);
      }
    }
    $customer['address'] = [
      'address1' => $address
        ->getAddressLine1(),
      'address2' => $address
        ->getAddressLine2(),
      'city' => $address
        ->getLocality(),
      'province_code' => $address
        ->getAdministrativeArea(),
      'postal_code' => $address
        ->getPostalCode(),
      'country_code' => $address
        ->getcountryCode(),
    ];
    foreach ($customer['address'] as $key => $value) {
      if ($value === NULL) {
        unset($customer['address'][$key]);
      }
    }
  }
  return $customer;
}