You are here

function commerce_cart_order_convert in Commerce Core 7

Converts an anonymous shopping cart order to an authenticated cart.

Parameters

$order: The anonymous order to convert to an authenticated cart.

$account: The user account the order will belong to.

Return value

The updated order's wrapper or FALSE if the order was not converted, meaning it was not an anonymous cart order to begin with.

1 call to commerce_cart_order_convert()
commerce_cart_user_login in modules/cart/commerce_cart.module
Implements hook_user_login().

File

modules/cart/commerce_cart.module, line 1052
Implements the shopping cart system and add to cart features.

Code

function commerce_cart_order_convert($order, $account) {

  // Only convert orders that are currently anonmyous orders.
  if ($order->uid == 0) {

    // Update the uid and e-mail address to match the current account since
    // there currently is no way to specify a custom e-mail address per order.
    $order->uid = $account->uid;
    $order->mail = $account->mail;

    // Update the uid of any referenced customer profiles.
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
    foreach (field_info_instances('commerce_order', $order->type) as $field_name => $instance) {
      $field_info = field_info_field($field_name);
      if ($field_info['type'] == 'commerce_customer_profile_reference') {
        if ($order_wrapper->{$field_name} instanceof EntityListWrapper) {
          foreach ($order_wrapper->{$field_name} as $delta => $profile_wrapper) {
            if ($profile_wrapper->uid
              ->value() == 0) {
              $profile_wrapper->uid = $account->uid;
              $profile_wrapper
                ->save();
            }
          }
        }
        elseif (!is_null($order_wrapper->{$field_name}
          ->value()) && $order_wrapper->{$field_name}->uid
          ->value() == 0) {
          $order_wrapper->{$field_name}->uid = $account->uid;
          $order_wrapper->{$field_name}
            ->save();
        }
      }
    }

    // Allow other modules to operate on the converted order and then save.
    module_invoke_all('commerce_cart_order_convert', $order_wrapper, $account);
    $order_wrapper
      ->save();
    return $order_wrapper;
  }
  return FALSE;
}