You are here

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

@inheritdoc

Overrides CustomerHandlerInterface::addOrUpdateCustomer

File

src/CustomerHandler.php, line 60

Class

CustomerHandler
Customer handler.

Namespace

Drupal\mailchimp_ecommerce

Code

public function addOrUpdateCustomer($customer) {
  try {
    $store_id = mailchimp_ecommerce_get_store_id();
    if (empty($store_id)) {
      throw new \Exception('Cannot add or update a customer without a store ID.');
    }

    /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
    $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
    try {
      if (!empty($mc_ecommerce
        ->getCustomer($store_id, $customer['id']))) {
        $mc_ecommerce
          ->updateCustomer($store_id, $customer);
      }
    } catch (\Exception $e) {
      if ($e
        ->getCode() == 404) {

        // Customer doesn't exist; add a new customer.
        $mc_ecommerce
          ->addCustomer($store_id, $customer);
      }
      else {

        // An actual error occurred; pass on the exception.
        throw new \Exception($e
          ->getMessage(), $e
          ->getCode(), $e);
      }
    }
  } catch (\Exception $e) {
    mailchimp_ecommerce_log_error_message('Unable to add a customer: ' . $e
      ->getMessage());
    drupal_set_message($e
      ->getMessage(), 'error');
  }
}