You are here

function commerce_wishlist_upgrade_from_1x_to_3x in Commerce Wishlist 7.3

Upgrade from 1.x to 3.x.

1 call to commerce_wishlist_upgrade_from_1x_to_3x()
commerce_wishlist_update_7300 in ./commerce_wishlist.install
Convert wish lists to use orders.

File

./commerce_wishlist.install, line 178
Installation hooks.

Code

function commerce_wishlist_upgrade_from_1x_to_3x(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_uid'] = 0;
    $sandbox['max'] = db_query('SELECT MAX(DISTINCT uid) FROM {commerce_wishlist}')
      ->fetchField();
    $sandbox['total'] = db_query('SELECT COUNT(DISTINCT uid) FROM {commerce_wishlist}')
      ->fetchField();
  }

  // Get the entries we are updating in this iteration.
  $user_ids = db_select('commerce_wishlist', 'cw')
    ->distinct()
    ->fields('cw', array(
    'uid',
  ))
    ->condition('uid', $sandbox['current_uid'], '>')
    ->range(0, 50)
    ->orderBy('uid', 'ASC')
    ->execute()
    ->fetchAll();

  // Add or create the items..
  if ($user_ids) {
    foreach ($user_ids as $user) {
      $uid = $user->uid;
      $wishlist_products = db_select('commerce_wishlist', 'cw')
        ->fields('cw')
        ->condition('uid', $uid)
        ->execute();
      $wishlist_order = commerce_wishlist_order_new($uid);
      foreach ($wishlist_products as $wishlist_product) {
        $product = commerce_product_load($wishlist_product->product_id);
        $line_item = commerce_product_line_item_new($product);
        $line_item->created = time();
        $line_item->quantity = !empty($wishlist_product->quantity) ? (int) $wishlist_product->quantity : 1;
        $line_item->commerce_display_path[LANGUAGE_NONE][0] = array(
          'value' => 'node/' . $wishlist_product->nid,
        );

        // Set the incoming line item's order_id.
        $line_item->order_id = $wishlist_order->order_id;

        // Save the incoming line item now so we get its ID.
        commerce_line_item_save($line_item);

        // Add it to the order's line item reference value.
        $wishlist_order->commerce_line_items[LANGUAGE_NONE][] = array(
          'line_item_id' => $line_item->line_item_id,
        );
      }

      // Save the updated order.
      commerce_order_save($wishlist_order);
      $sandbox['progress']++;
      $sandbox['current_uid'] = $uid;
    }
  }
  else {
    $sandbox['#finished'] = 1;
    return;
  }
  $sandbox['#finished'] = empty($sandbox['total']) ? 1 : $sandbox['progress'] / $sandbox['total'];
}