You are here

function mailchimp_ecommerce_delete_order in Mailchimp E-Commerce 7

Deletes an Order from the current Mailchimp store.

Parameters

string $order_id: The Order ID.

1 call to mailchimp_ecommerce_delete_order()
mailchimp_ecommerce_commerce_commerce_order_delete in modules/mailchimp_ecommerce_commerce/mailchimp_ecommerce_commerce.module
Implements hook_commerce_order_delete().

File

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

Code

function mailchimp_ecommerce_delete_order($order_id) {
  try {
    $store_id = mailchimp_ecommerce_get_store_id();
    if (empty($store_id)) {
      throw new Exception('Cannot delete an order without a store ID.');
    }

    // Get customer and order arrays.
    $order = commerce_order_load($order_id);
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    $mc_order = _mailchimp_ecommerce_commerce_build_order($order_wrapper);
    $customer = $mc_order['customer'];
    $order = $mc_order['order_data'];
    if (mailchimp_ecommerce_use_queue()) {
      mailchimp_ecommerce_create_queue_item([
        'op' => 'deleteOrder',
        'order_id' => $order_id,
        'store_id' => $store_id,
        'customer' => $customer,
        'order' => $order,
      ]);
    }
    else {
      $list_id = mailchimp_ecommerce_get_list_id();

      /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
      $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
      $mc_ecommerce
        ->deleteOrder($store_id, $order_id);

      // Pull member information to get member status.
      $order = $mc_ecommerce
        ->getOrder($store_id, $order_id);

      // Decrement customer totals.
      $remote_customer = mailchimp_ecommerce_get_customer($order->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 - $order['order_total'];
      $mc_ecommerce
        ->updateCustomer($store_id, $customer);
    }
  } catch (Exception $e) {
    if ($e
      ->getCode() == 404) {

      // Order doesn't exist; no need to log an error.
    }
    else {
      mailchimp_ecommerce_log_error_message('Unable to delete an order: ' . $e
        ->getMessage());
      mailchimp_ecommerce_show_error($e
        ->getMessage());
    }
  }
}