You are here

function uc_importer_orders in Ubercart 5

Specific order importer for a certain osCommerce site.

File

uc_importer/uc_importer.module, line 1221
XML product importer and exporter.

Code

function uc_importer_orders($xml) {
  $id_map = array(
    'statuses' => array(
      1 => 'pending',
      20 => 'in_checkout',
      30 => 'pending',
      40 => 'processing',
      50 => 'processing',
      60 => 'completed',
      70 => 'completed',
      -20 => 'canceled',
      -10 => 'in_checkout',
    ),
    'countries' => array(
      38 => 124,
      // Canada
      138 => 484,
      // Mexico
      223 => 840,
      // United States
      240 => 158,
      // Taiwan
      241 => 826,
      // United Kingdom
      242 => 276,
    ),
    'orders' => array(),
    'products' => array(),
  );
  $store = simplexml_load_string($xml);
  foreach ($store->orders->order as $order_data) {
    $uid = 0;
    $user = user_load(array(
      'mail' => (string) $order_data->primary_email,
    ));
    if ($user) {
      $uid = $user->uid;
    }
    $status = $id_map['statuses'][$order_data->order_status];
    $order = uc_order_new($uid, $status);
    $order_fields = array(
      'order_status',
      'order_total',
      'primary_email',
      'delivery_first_name',
      'delivery_last_name',
      'delivery_phone',
      'delivery_company',
      'delivery_street1',
      'delivery_street2',
      'delivery_city',
      'delivery_zone',
      'delivery_postal_code',
      'billing_first_name',
      'billing_last_name',
      'billing_phone',
      'billing_company',
      'billing_street1',
      'billing_street2',
      'billing_city',
      'billing_zone',
      'billing_postal_code',
      'payment_method',
      'modified',
      'created',
    );
    foreach ($order_fields as $field) {
      $order->{$field} = (string) $order_data->{$field};
    }
    $order->delivery_country = $id_map['countries'][(string) $order_data->delivery_country];
    $order->billing_country = $id_map['countries'][(string) $order_data->billing_country];
    db_query("UPDATE {uc_orders} SET uid = %d, order_status = '%s', order_total = %f, primary_email = '%s', " . "delivery_first_name = '%s', delivery_last_name = '%s', delivery_phone = '%s', " . "delivery_company = '%s', delivery_street1 = '%s', delivery_street2 = '%s', " . "delivery_city = '%s', delivery_zone = %d, delivery_postal_code = '%s', delivery_country = %d, " . "billing_first_name = '%s', billing_last_name = '%s', billing_phone = '%s', " . "billing_company = '%s', billing_street1 = '%s', billing_street2 = '%s', " . "billing_city = '%s', billing_zone = %d, billing_postal_code = '%s', billing_country = %d, " . "payment_method = '%s', modified = %d, created = %d WHERE order_id = %d", $order->uid, $order->order_status, $order->order_total, $order->primary_email, $order->delivery_first_name, $order->delivery_last_name, $order->delivery_phone, $order->delivery_company, $order->delivery_street1, $order->delivery_street2, $order->delivery_city, $order->delivery_zone, $order->delivery_postal_code, is_null($order->delivery_country) || $order->delivery_country == 0 ? variable_get('uc_store_country', 840) : $order->delivery_country, $order->billing_first_name, $order->billing_last_name, $order->billing_phone, $order->billing_company, $order->billing_street1, $order->billing_street2, $order->billing_city, $order->billing_zone, $order->billing_postal_code, is_null($order->billing_country) || $order->billing_country == 0 ? variable_get('uc_store_country', 840) : $order->billing_country, $order->payment_method, $order->modified, $order->created, $order->order_id);
    foreach ($order_data->products->product as $product_data) {
      $product = new stdClass();
      $product->nid = 0;
      $product_fields = array(
        'qty',
        'name',
        'manufacturer',
        'model',
        'cost',
        'price',
        'weight',
      );
      foreach ($product_fields as $field) {
        $product->{$field} = (string) $product_data->{$field};
      }
      $product->data = unserialize((string) $product_data->data);
      uc_order_product_save($order->order_id, $product);
    }
    $comments_types = array();
    $comments_values = array();
    foreach ($order->comments->comment as $comment) {
      $comments_types[] = "(%d,%d,'%s',%d,%d,%d)";
      $comments_values[] = $order->order_id;
      $commenter = user_load(array(
        'name' => (string) $comment->user,
      ));
      if ($commenter) {
        $comments_values[] = $commenter->uid;
      }
      else {
        $comments_values[] = 0;
      }
      $comments_values[] = (string) $comment->message;
      $comments_values[] = (string) $comment->order_status;
      $comments_values[] = (int) $comment->notified;
      $comments_values[] = (int) $comment->created;
    }
    if (count($comments_values)) {
      db_query("INSERT INTO {uc_order_comments} (order_id, uid, message, order_status, notified, created) VALUES " . implode(',', $comments_types), $comments_values);
    }
    $comments_types = array();
    $comments_values = array();
    foreach ($order->admin_comments->comment as $comment) {
      $comments_types[] = "(%d,%d,'%s',%d)";
      $comments_values[] = $order->order_id;
      $commenter = user_load(array(
        'name' => (string) $comment->user,
      ));
      if ($commenter) {
        $comments_values[] = $commenter->uid;
      }
      else {
        $comments_values[] = 0;
      }
      $comments_values[] = (string) $comment->message;
      $comments_values[] = (int) $comment->created;
    }
    if (count($comments_values)) {
      db_query("INSERT INTO {uc_order_admin_comments} (order_id, uid, message, created) VALUES " . implode(',', $comments_types), $comments_values);
    }
  }
}