You are here

function commerce_wishlist_upgrade_from_2x_to_3x in Commerce Wishlist 7.3

Upgrades from Commerce Wishlist 2.x to 3.x

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

File

./commerce_wishlist.install, line 244
Installation hooks.

Code

function commerce_wishlist_upgrade_from_2x_to_3x(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_id'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT wishlist_id) FROM {commerce_wishlist}')
      ->fetchField();
  }

  // Get our wish lists.
  $wishlist_ids = db_select('commerce_wishlist', 'cw')
    ->fields('cw', array(
    'wishlist_id',
    'uid',
  ))
    ->condition('wishlist_id', $sandbox['current_id'], '>')
    ->range(0, 50)
    ->orderBy('wishlist_id', 'ASC')
    ->execute()
    ->fetchAll();

  // Update the coupons.
  if ($wishlist_ids) {
    foreach ($wishlist_ids as $wishlist) {
      $product_ids = db_select('commerce_wishlist_item', 'cwi')
        ->fields('cwi')
        ->condition('wishlist_id', $wishlist->wishlist_id)
        ->execute()
        ->fetchAll();
      if (!$product_ids) {
        $sandbox['progress']++;
        $sandbox['current_id'] = $wishlist->wishlist_id;
        continue;
      }
      $wishlist_order = commerce_wishlist_order_new($wishlist->uid);
      foreach ($product_ids as $wishlist_product) {
        $product = commerce_product_load($wishlist_product->product_id);
        $line_item = commerce_product_line_item_new($product, 1);
        $line_item->created = $wishlist_product->added;
        $line_item->quantity = $wishlist_product->quantity > 1 ? $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_id'] = $wishlist->coupon_id;
    }
  }
  else {
    $sandbox['#finished'] = 1;
    return;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}