You are here

function commerce_reorder_helper in Commerce Reorder 7

Same name and namespace in other branches
  1. 7.2 commerce_reorder.module \commerce_reorder_helper()

Helper function to reorder a previous order.

Parameters

object $order: The commerce_order object to reorder.

object $account: The user account to assign the order to.

array $settings: An optional array that may contain the following values:

  • copy_profiles: An array of profile types from commerce_customer_profile_types() to be copied to the order. It will look for the fields 'commerce_customer_TYPE' on the order. e.g. array('shipping' => 'shipping').
2 calls to commerce_reorder_helper()
commerce_reorder_handler_field_commerce_reorder_button::views_form_submit in includes/views/handlers/commerce_reorder_handler_field_commerce_reorder_button.inc
commerce_reorder_reorder_callback in ./commerce_reorder.module
Perform the reorder action for the operations menu

File

./commerce_reorder.module, line 92
Allows users to create a new order from their order history.

Code

function commerce_reorder_helper($order = NULL, $account = NULL, $settings = array()) {
  $profile_types = array_keys(commerce_customer_profile_types());
  foreach ($profile_types as $type) {
    $default_profile_options[$type] = FALSE;
  }

  // Set the default copy options.
  $settings = (array) $settings + array(
    'copy_profiles' => $default_profile_options,
  );
  if (!isset($order)) {
    return;
  }
  if (empty($account)) {
    global $user;
    $account = $user;
  }

  // Get the line items of the order.
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
    if (in_array($line_item_wrapper->type
      ->value(), commerce_product_line_item_types())) {
      $product = $line_item_wrapper->commerce_product
        ->value();
      if ($product->status) {
        $line_item = $line_item_wrapper
          ->value();

        // Generate a line item product based in the current one.
        $new_line_item = commerce_product_line_item_new($product, $line_item->quantity, $line_item->order_id, $line_item->data, $line_item->type);

        // Merge both line items to get the fields (if any).
        $new_line_item = (object) array_merge((array) $line_item, (array) $new_line_item);

        // @TODO Add option to combine / add separately.
        // See @commerce_cart_product_add
        commerce_cart_product_add($account->uid, $new_line_item);
      }
      else {
        drupal_set_message(t('Some products weren\'t copied to the cart as they aren\'t currently available'), 'status', FALSE);
      }
    }
    else {
      drupal_set_message(t('Some products weren\'t copied to the cart as they aren\'t currently available'), 'status', FALSE);
    }
  }

  // Copy over profiles if they are set.
  $cart = $cart_wrapper = null;
  foreach ($settings['copy_profiles'] as $profile_type => $copy) {
    if ($profile_type !== $copy) {
      continue;
    }
    if (!isset($cart)) {
      $cart = commerce_cart_order_load($account->uid);
      $cart_wrapper = entity_metadata_wrapper('commerce_order', $cart);
    }
    $field_name = 'commerce_customer_' . $profile_type;
    if (isset($order_wrapper->{$field_name}) && isset($cart_wrapper->{$field_name})) {
      $cart_wrapper->{$field_name}
        ->set($order_wrapper->{$field_name}
        ->value()->profile_id);
    }
  }
  if (isset($cart_wrapper)) {
    $cart_wrapper
      ->save();
  }
}