2

I am trying to import orders from xls file to drupal commerce store.Is there any existing module for this, I found commerce_migrate and commerce_feeds module I am not sure if they can be helpful here could not find if any of the two module provides this functionality as not mentioned on project page.

3 Answers 3

2

I wrote a script to import orders. Here is the workflow I followed

  1. imported all users using user_import module, the orders belongs to.
  2. Created a custom database tables with columns needed to make an order.
  3. Imported the excel/csv file to custom table after formating data.
  4. Created a script to import orders and create customer profile linked to a order

Here is the script

/*
 * Helper function to facilate order import functinality.
 */

function mymodule_name_order_import_($email, $product_sku, $order_created_date, $quantity, $status, $billing_fields, $payment_method) {

  // Get product id.
  $product = commerce_product_load_by_sku($product_sku);
  $product_id = $product->product_id;

  $user = user_load_by_mail($email);
  $uid = $user->uid;

  if (!empty($uid)) {

    // Set order status.
    if ($status == "Pending") {
      $status = "pending";
    }if ($status == "Completed") {
      $status = "completed";
    }

    // Keeping order status to pending. -- Need to Change as per order. -- 
    $order = commerce_order_new($uid, $status);

// Save the order so its ID is assigned.
    commerce_order_save($order);

// Order created date in time stamp.
    $date = DateTime::createFromFormat('m/d/Y', $order_created_date);
    $order->created = $date->getTimestamp();

    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

//add products to order by ids array
    $product = commerce_product_load($product_id);

// Create a line item with quantity 1 and this product.
    $line_item = commerce_product_line_item_new($product, $quantity, $order->order_id);

// Save the line item to get its ID.
    commerce_line_item_save($line_item);

// Add the line item to the order using the wrapper.
    $order_wrapper->commerce_line_items[] = $line_item;

// Get Billing Profile.
    $profile_billing = mymodule_name_order_import_map_billing_fields($uid, $billing_fields);

// Map billing address to order.
    $order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'] = $profile_billing->profile_id;

// Get Shipping Profile.
    $profile_shipping = mymodule_name_order_import_map_shipping_fields($uid, $billing_fields);

// Map billing address to order.
    $order->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id'] = $profile_shipping->profile_id;

// Payment Method
    if ($product_sku == "25PDVDP") {
      $order->data['payment_method'] = "payment_commerce_2|commerce_payment_payment_commerce_2";
    }
    elseif ($product_sku != "25PDVDP") {
      if ($payment_method == "Credit Card") {
        $order->data['payment_method'] = "credit_cart|custom_import";
      }
      if ($payment_method == "PayPal") {
        $order->data['payment_method'] = "paypal|custom_import";
      }
      else {
        $order->data['payment_method'] = "na|custom_import";
      }
    }

// Save the order.
    commerce_order_save($order);
  }
  else {
    print "This user doesn't exists : " . $email;
  }
}

/**
 * Helper function to map billing fields.
 * @param $uid user id of user to make billing profile for.
 */
function mymodule_name_order_import_map_billing_fields($uid, $billing_fields) {
  $profile_billing = commerce_customer_profile_new('billing', $uid);
  $profile_billing->commerce_customer_address = array(
    LANGUAGE_NONE => array(
      0 => addressfield_default_values()
    )
  );

// Full Name(Required)
  $profile_billing->commerce_customer_address[LANGUAGE_NONE][0]['name_line'] = $billing_fields['name_line'];

// Country(Required)
  $profile_billing->commerce_customer_address[LANGUAGE_NONE][0]['country'] = $billing_fields['country'];

// Address 1(Required)
  $profile_billing->commerce_customer_address[LANGUAGE_NONE][0]['thoroughfare'] = $billing_fields['thoroughfare'];

// Address 2
  $profile_billing->commerce_customer_address[LANGUAGE_NONE][0]['premise'] = $billing_fields['premise'];

// City(Required)
  $profile_billing->commerce_customer_address[LANGUAGE_NONE][0]['locality'] = $billing_fields['locality'];

// State(Required)
  $profile_billing->commerce_customer_address[LANGUAGE_NONE][0]['administrative_area'] = $billing_fields['administrative_area'];

// Postal Code(Required)
  $profile_billing->commerce_customer_address[LANGUAGE_NONE][0]['postal_code'] = $billing_fields['postal_code'];

//Business name(Required)
  $profile_billing->field_business_name[LANGUAGE_NONE][0]['value'] = $billing_fields['field_business_name'];

// Who runs your front desk?(Required)
  $profile_billing->field_who_runs_your_front_desk_[LANGUAGE_NONE][0]['value'] = $billing_fields['field_who_runs_your_front_desk_'];

// Email address for the front desk person(Required)
  $profile_billing->field_email_address_front_desk[LANGUAGE_NONE][0]['value'] = $billing_fields['field_email_address_front_desk'];

// Phone(Required)
  $profile_billing->field_phone[LANGUAGE_NONE][0]['value'] = $billing_fields['field_phone'];

// Save billing profile.
  commerce_customer_profile_save($profile_billing);

  return $profile_billing;
}

/**
 * Helper function to map billing fields.
 * @param $uid user id of user to make shipping profile for.
 */
function mymodule_name_order_import_map_shipping_fields($uid, $billing_fields) {
  $profile_shipping = commerce_customer_profile_new('shipping', $uid);
  $profile_shipping->commerce_customer_address = array(
    LANGUAGE_NONE => array(
      0 => addressfield_default_values()
    )
  );

// Full Name(Required)
  $profile_shipping->commerce_customer_address[LANGUAGE_NONE][0]['name_line'] = $billing_fields['name_line'];

// Country(Required)
  $profile_shipping->commerce_customer_address[LANGUAGE_NONE][0]['country'] = $billing_fields['country'];

// Address 1(Required)
  $profile_shipping->commerce_customer_address[LANGUAGE_NONE][0]['thoroughfare'] = $billing_fields['thoroughfare'];

// Address 2
  $profile_shipping->commerce_customer_address[LANGUAGE_NONE][0]['premise'] = $billing_fields['premise'];

// City(Required)
  $profile_shipping->commerce_customer_address[LANGUAGE_NONE][0]['locality'] = $billing_fields['locality'];

// State(Required)
  $profile_shipping->commerce_customer_address[LANGUAGE_NONE][0]['administrative_area'] = $billing_fields['administrative_area'];

// Postal Code(Required)
  $profile_shipping->commerce_customer_address[LANGUAGE_NONE][0]['postal_code'] = $billing_fields['postal_code'];

// You sould pass the entire $profile object as parameter.
  commerce_customer_profile_save($profile_shipping);

  return $profile_shipping;
}

function jh_order_import_run($jh_product_sku = '') {

  //$jh_product_sku = '25PDVDP';
  $result = db_select('jh_order_import', 'joi')->fields('joi')->condition('product_sku', $jh_product_sku, '=')->execute()->fetchAll();

  foreach ($result as $key => $value) {
    echo "<pre>";
    print_r($value);
    $country = array('United States' => 'US', 'Canada' => 'CA',
      'Australia' => 'AU', 'United Kingdom' => 'GB', 'South Africa' => 'ZA');
    $jh_bill = array('name_line' => $value->full_name, 'country' => $country[$value->country],
      'thoroughfare' => $value->billing_address_1,
      'premise' => $value->billing_address_2,
      'locality' => $value->billing_city,
      'administrative_area' => $value->billing_state,
      'postal_code' => $value->billing_postal_code,
      'field_business_name' => $value->billing_bussiness_name,
      'field_who_runs_your_front_desk_' => $value->billing_front_desk_name,
      'field_email_address_front_desk' => $value->billing_front_desk_email,
      'field_phone' => $value->billing_phone,
    );
    $payment_method = $value->payment_methods;
    mymodule_name_order_import_($value->email, $value->product_sku, $value->order_date, $value->quantity, $value->status, $jh_bill, $payment_method);
    print "imported " . $key;
  }
}

NOTE: the above script is use case specific, so it will need modifications per need, but might be helpful to start with. I am using drupal commerce API's to import orders, customer profiles that's all.

2
  • Two quick questions: 1) Why did you import from CSV into SQL? Why not simply have your script read the CSV with the line items directly? 2) How do you actually get your script to 'run'? IOW: where is it executed from? --TIA, JC
    – jchwebdev
    Commented Oct 29, 2014 at 18:36
  • @jchwebdev provided data was not a result of drupal commerce, reason I have to import it into custom tables and then create orders using commerce API's. the above script is simply a set of functions which can be placed in a temporary custom module. Functions expect the raw data imported in custom tables and loop over through it :)
    – arpitr
    Commented Oct 30, 2014 at 13:44
2

You can use latest feeds module (currently 7.x-2.x-dev from 2014-Dec-10) which contains EXPERIMENTAL importers to import entities!

Luckily, Orders are entities. The steps you need for this (using feeds of course) are:

  1. Import drupal users (if they do not exist and they are needed)
  2. Import commerce products (if they do not exist)
  3. (Optional) Import product displays (nodes) for the commerce products.
  4. Import commerce orders (you need at least the owner uid)
  5. Import commerce line items of your type for each order (eg commerce products, commerce profiles, commerce shipping etc). Notice that the match here is with the order_id.

So you may need at least 1 importer for the Orders and 1 additional importer for each line item type (products, shipping, payments, customer profiles etc).

0

You can use the Feed Importer module with no bother. Just convert your XLS file to a CSV file. https://www.drupal.org/project/feed_import

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.