You are here

function commerce_product_batch_pre_calculate_sell_prices in Commerce Core 7

Sets a batch operation to pre-calculate product sell prices.

This function prepares and sets a batch to pre-calculate product sell prices based on the number of variations for each price and the number of products to calculate. It does not call batch_process(), so if you are not calling this function from a form submit handler, you must process yourself.

1 call to commerce_product_batch_pre_calculate_sell_prices()
commerce_product_pre_calculation_settings_form_submit in modules/product_pricing/includes/commerce_product_pricing_ui.admin.inc
Submit callback: process the product sell price pre-calculation form.

File

modules/product_pricing/commerce_product_pricing.module, line 419
Enables Rules based product sell price calculation for dynamic product pricing.

Code

function commerce_product_batch_pre_calculate_sell_prices() {

  // Create the batch array.
  $batch = array(
    'title' => t('Pre-calculating product sell prices'),
    'operations' => array(),
    'finished' => 'commerce_product_batch_pre_calculate_sell_prices_finished',
  );

  // Add operations based on the number of rule combinations and number of
  // products to be pre-calculated.
  $rule_keys = commerce_product_pre_calculation_rule_keys();

  // Count the number of products.
  $product_count = db_select('commerce_product', 'cp')
    ->fields('cp', array(
    'product_id',
  ))
    ->countQuery()
    ->execute()
    ->fetchField();

  // Create a "step" value based on the number of rule keys, i.e. how many
  // products to calculate at a time. This will roughly limit the number of
  // queries to 500 on any given batch operations.
  if (count($rule_keys) > 500) {
    $step = 1;
  }
  else {
    $step = min(round(500 / count($rule_keys)) + 1, $product_count);
  }

  // Add batch operations to pre-calculate every price.
  for ($i = 0; $i < $product_count; $i += $step) {

    // Ensure the maximum step doesn't go over the total number of rows for
    // accurate reporting later.
    if ($i + $step > $product_count) {
      $step = $product_count - $i;
    }
    $batch['operations'][] = array(
      '_commerce_product_batch_pre_calculate_sell_prices',
      array(
        $i,
        $step,
      ),
    );
  }
  batch_set($batch);
}