You are here

function commerce_cart_user_update in Commerce Core 7

Implements hook_user_update().

When a user account e-mail address is updated, update any shopping cart orders owned by the user account to use the new e-mail address.

File

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

Code

function commerce_cart_user_update(&$edit, $account, $category) {

  // If the e-mail address was changed...
  if (!empty($edit['original']->mail) && $account->mail != $edit['original']->mail) {

    // Load the user's shopping cart orders.
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'commerce_order', '=')
      ->propertyCondition('uid', $account->uid, '=')
      ->propertyCondition('status', array_keys(commerce_order_statuses(array(
      'cart' => TRUE,
    ))), 'IN');
    $result = $query
      ->execute();
    if (!empty($result['commerce_order'])) {
      foreach (commerce_order_load_multiple(array_keys($result['commerce_order'])) as $order) {
        if ($order->mail != $account->mail) {
          $order->mail = $account->mail;
          commerce_order_save($order);
        }
      }
    }
  }
}