function commerce_order_update_7101 in Commerce Core 7
Loads and resaves all the line items on the site, updating the unit price field to have proper component price amount values.
File
- modules/
order/ commerce_order.install, line 278
Code
function commerce_order_update_7101(&$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_line_item_id'] = 0;
$sandbox['max'] = db_query("SELECT COUNT(DISTINCT line_item_id) FROM {commerce_line_item}")
->fetchField();
}
// Load the next 50 line items.
$line_items = db_select('commerce_line_item', 'cli')
->fields('cli', array(
'line_item_id',
))
->condition('line_item_id', $sandbox['current_line_item_id'], '>')
->range(0, 50)
->orderBy('line_item_id', 'ASC')
->execute();
// Loop over the line items, loading, adjusting, and resaving each one.
foreach ($line_items as $line_item) {
$line_item = commerce_line_item_load($line_item->line_item_id);
// If the commerce_unit_price field has a components array, multiply its
// amounts by the proper value for its currency.
if (!empty($line_item->commerce_unit_price)) {
foreach ($line_item->commerce_unit_price as $langcode => &$data) {
foreach ($data as $delta => &$item) {
if (!empty($item['data']['components'])) {
foreach ($item['data']['components'] as $key => &$component) {
$component['price']['amount'] = commerce_currency_decimal_to_amount($component['price']['amount'], $component['price']['currency_code'], FALSE);
}
}
}
}
}
commerce_line_item_save($line_item);
$sandbox['progress']++;
$sandbox['current_line_item_id'] = $line_item->line_item_id;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
return t('All line items have been loaded and saved with updated price component arrays.');
}