You are here

function _commerce_kickstart_create_order in Commerce Kickstart 7.2

1 call to _commerce_kickstart_create_order()
_commerce_kickstart_example_user in ./commerce_kickstart.install_callbacks.inc
BatchAPI callback.

File

./commerce_kickstart.install_callbacks.inc, line 385
Contains Batch API callbacks used during installation.

Code

function _commerce_kickstart_create_order($uid, $items_number, $is_cart = FALSE) {

  // Create order as a cart.
  $order = commerce_order_new($uid, 'cart');
  commerce_order_save($order);
  $message = message_create('commerce_order_created', array(
    'uid' => $uid,
  ));
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->message_commerce_order
    ->set($order);
  $wrapper
    ->save();

  // Add products to the cart.
  $line_items = array();
  $products = entity_load('commerce_product');
  $products_key = array_keys($products);
  $products_number = count($products_key);
  $count = $products_number < $items_number ? $products_number : $items_number;
  for ($i = 0; $i < $count; $i++) {
    $product = $products[$products_key[rand(0, $products_number - 1)]];
    $line_item = commerce_product_line_item_new($product, 1, $order->order_id);
    commerce_line_item_save($line_item);
    $line_items[] = array(
      'line_item_id' => $line_item->line_item_id,
    );
    $message = message_create('commerce_order_cart_add', array(
      'uid' => $uid,
    ));
    $wrapper = entity_metadata_wrapper('message', $message);
    $wrapper->message_commerce_order
      ->set($order);
    $wrapper->message_commerce_line_item
      ->set($line_item);
    $wrapper
      ->save();
  }
  $order->commerce_line_items[LANGUAGE_NONE] = $line_items;

  // Stop here for cart creation.
  if ($is_cart) {
    commerce_order_save($order);
    return;
  }

  // Attach customer profile.
  _commerce_kickstart_update_order_status($order, 'checkout_checkout');
  $billing_profile_id = commerce_addressbook_get_default_profile_id($uid, 'billing');
  $shippin_profile_id = commerce_addressbook_get_default_profile_id($uid, 'shipping');
  if (!empty($billing_profile_id)) {
    $order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'] = $billing_profile_id;
  }
  if (!empty($shippin_profile_id)) {
    $order->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id'] = $shippin_profile_id;
  }

  //@TODO Add shipping service.
  _commerce_kickstart_update_order_status($order, 'checkout_shipping');

  // Proceed to payment.
  _commerce_kickstart_update_order_status($order, 'checkout_review');
  $order->data['payment_method'] = 'commerce_payment_example|commerce_payment_commerce_payment_example';
  $order->data['commerce_payment_example'] = array(
    'credit_card' => array(
      'number' => '4111111111111111',
      'exp_month' => '06',
      'exp_year' => '2012',
    ),
  );
  commerce_order_save($order);
  $rule = rules_config_load('commerce_payment_commerce_payment_example');
  $payment_method = commerce_payment_method_load('commerce_payment_example');
  $method_instance_id = commerce_payment_method_instance_id('commerce_payment_example', $rule);
  $method_instance = commerce_payment_method_instance_load($method_instance_id);
  $transaction = commerce_payment_example_transaction($method_instance, $order, $order->commerce_order_total[LANGUAGE_NONE][0], '');
  $message = message_create('commerce_order_payment_entered', array(
    'uid' => $uid,
  ));
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->message_commerce_order
    ->set($order);
  $wrapper->message_commerce_payment
    ->set($transaction);
  $wrapper
    ->save();
  _commerce_kickstart_update_order_status($order, 'checkout_payment');
  _commerce_kickstart_update_order_status($order, 'checkout_complete');
  _commerce_kickstart_update_order_status($order, 'pending');
}