function commerce_product_pre_calculate_sell_prices in Commerce Core 7
Pre-calculates sell prices for qualifying products based on valid rule configurations on the "Calculating product sell price" event.
Parameters
$from: For calculating prices for a limited number of products at a time, specify the range's "from" amount.
$count: For calculating prices for a limited number of products at a time, specify the range's "count" amount.
Return value
The number of pre-calculated prices created.
1 call to commerce_product_pre_calculate_sell_prices()
- _commerce_product_batch_pre_calculate_sell_prices in modules/
product_pricing/ commerce_product_pricing.module - Calculates pre-calculation using the given range values and updates the batch with processing information.
File
- modules/
product_pricing/ commerce_product_pricing.module, line 265 - Enables Rules based product sell price calculation for dynamic product pricing.
Code
function commerce_product_pre_calculate_sell_prices($from = NULL, $count = 1) {
$total = 0;
// Load the sell price calculation event.
$event = rules_get_cache('event_commerce_product_calculate_sell_price');
// If there are no rule configurations, leave without further processing.
if (empty($event)) {
return array();
}
// If a range was specified, query just those products...
if (!is_null($from)) {
$query = db_query_range("SELECT product_id FROM {commerce_product}", $from, $count);
}
else {
// Otherwise load all products.
$query = db_query("SELECT product_id FROM {commerce_product}");
}
while ($product_id = $query
->fetchField()) {
$product = commerce_product_load($product_id);
// If the product is valid for pre-calculation...
if (commerce_product_valid_pre_calculation_product($product)) {
// For each rule key (i.e. set of applicable rule configurations)...
foreach (commerce_product_pre_calculation_rule_keys() as $key) {
// Build a product line item and Rules state object.
$line_item = commerce_product_line_item_new($product);
$state = new RulesState();
$vars = $event
->parameterInfo(TRUE);
$state
->addVariable('commerce_line_item', $line_item, $vars['commerce_line_item']);
// For each Rule signified by the current key...
foreach (explode('|', $key) as $name) {
// Load the Rule and "fire" it, evaluating its actions without doing
// any condition evaluation.
if ($rule = rules_config_load($name)) {
$rule
->fire($state);
}
}
// Also fire any Rules that weren't included in the key because they
// don't have any conditions.
foreach ($event as $rule) {
if (count($rule
->conditions()) == 0) {
$rule
->fire($state);
}
}
// Build the record of the pre-calculated price and write it.
$wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
if (!is_null($wrapper->commerce_unit_price
->value())) {
$record = array(
'module' => 'commerce_product_pricing',
'module_key' => $key,
'entity_type' => 'commerce_product',
'entity_id' => $product_id,
'field_name' => 'commerce_price',
'language' => !empty($product->language) ? $product->language : LANGUAGE_NONE,
'delta' => 0,
'amount' => round($wrapper->commerce_unit_price->amount
->value()),
'currency_code' => $wrapper->commerce_unit_price->currency_code
->value(),
'data' => $wrapper->commerce_unit_price->data
->value(),
'created' => time(),
);
// Save the price and increment the total if successful.
if (drupal_write_record('commerce_calculated_price', $record) == SAVED_NEW) {
$total++;
}
}
}
}
}
return $total;
}