You are here

function commerce_order_update_7102 in Commerce Core 7

Loads and resaves all the orders on the site to rebuild the order total price component arrays.

File

modules/order/commerce_order.install, line 330

Code

function commerce_order_update_7102(&$sandbox) {

  // Ensure there are no stale prices in the field cache.
  field_cache_clear();

  // Establish the progress variables.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_order_id'] = 0;
    $sandbox['max'] = db_query("SELECT COUNT(DISTINCT order_id) FROM {commerce_order}")
      ->fetchField();
  }

  // Load the next 50 orders.
  $orders = db_select('commerce_order', 'co')
    ->fields('co', array(
    'order_id',
  ))
    ->condition('order_id', $sandbox['current_order_id'], '>')
    ->range(0, 50)
    ->orderBy('order_id', 'ASC')
    ->execute();

  // Loop over the orders, loading and resaving each one.
  foreach ($orders as $order) {
    $order = commerce_order_load($order->order_id);

    // Save the order as a new revision with an update log message.
    $order->revision = TRUE;
    $order->log = t('Order updated for 7.0-1.0-beta3 price component changes.');
    commerce_order_save($order);
    $sandbox['progress']++;
    $sandbox['current_order_id'] = $order->order_id;
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  return t('All orders have been loaded and saved with updated price component arrays.');
}