You are here

function mailchimp_ecommerce_queue_process in Mailchimp E-Commerce 7

Worker function to process a Mailchimp eCommerce queue item.

Parameters

array $item: A Drupal Queue item array.

1 string reference to 'mailchimp_ecommerce_queue_process'
mailchimp_ecommerce_cron_queue_info in ./mailchimp_ecommerce.module
Implements hook_queue_info().

File

./mailchimp_ecommerce.module, line 93
Mailchimp eCommerce core functionality.

Code

function mailchimp_ecommerce_queue_process($item) {
  $list_id = mailchimp_ecommerce_get_list_id();

  /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
  $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
  $customer = NULL;
  if (isset($item['customer'])) {
    $customer = $item['customer'];
    $store_id = mailchimp_ecommerce_get_store_id();
    if (empty($store_id)) {
      return;
    }
    if (!mailchimp_ecommerce_validate_customer($customer) && !$customer['email_address']) {
      return;
    }

    // Pull member information to get member status.
    $memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
    $customer['opt_in_status'] = isset($memberinfo->status) && $memberinfo->status == 'subscribed' ? TRUE : FALSE;
  }
  switch ($item['op']) {
    case 'addCart':
    case 'updateCart':
      $mc_ecommerce
        ->{$item['op']}($item['store_id'], $item['cart_id'], $customer, $item['cart']);
      break;
    case 'deleteCart':
      $mc_ecommerce
        ->{$item['op']}($item['store_id'], $item['cart_id']);
      break;
    case 'addCartLine':
    case 'updateCartLine':
      $mc_ecommerce
        ->{$item['op']}($item['store_id'], $item['cart_id'], $item['line_id'], $item['product']);
      break;
    case 'deleteCartLine':
      $mc_ecommerce
        ->{$item['op']}($item['store_id'], $item['cart_id'], $item['line_id']);
      break;
    case 'addOrder':

      // Pull member information to get member status.
      $memberinfo = mailchimp_get_memberinfo($list_id, $customer['email_address'], TRUE);
      $customer['opt_in_status'] = isset($memberinfo->status) && $memberinfo->status == 'subscribed' ? TRUE : FALSE;

      // Increment customer totals
      $remote_customer = mailchimp_ecommerce_get_customer($customer['id']);

      // Customer doesn't exist in the store.
      if (!$remote_customer) {
        return;
      }
      $customer['orders_count'] = $remote_customer->orders_count + 1;
      $customer['total_spent'] = $remote_customer->total_spent + $item['order']['order_total'];
      $mc_ecommerce
        ->{$item['op']}($item['store_id'], $item['order_id'], $customer, $item['order']);
      break;
    case 'updateOrder':
      $mc_ecommerce
        ->{$item['op']}($item['store_id'], $item['order_id'], $item['order']);
      break;
    case 'deleteOrder':
      $mc_ecommerce
        ->{$item['op']}($item['store_id'], $item['order_id'], $item['order']);

      // Decrement customer totals.
      $remote_customer = mailchimp_ecommerce_get_customer($customer['id']);

      // Customer doesn't exist in the store.
      if (!$remote_customer) {
        return;
      }
      $customer['orders_count'] = $remote_customer->orders_count - 1;
      $customer['total_spent'] = $remote_customer->total_spent - $item['order']['order_total'];
      $mc_ecommerce
        ->updateCustomer($store_id, $remote_customer);
      break;
  }
}