You are here

function commerce_product_valid_pre_calculation_rule in Commerce Core 7

Determines if a given rule configuration meets the requirements for price pre-calculation.

Parameters

$rule: A rule configuration belonging to the commerce_product_calculate_sell_price event.

$limit_validation: A boolean indicating whether or not the validation check should limit itself to the conditions in this function, effectively skipping the invocation of hook_commerce_product_valid_pre_calculation_rule().

Return value

TRUE or FALSE indicating whether or not the rule configuration is valid.

2 calls to commerce_product_valid_pre_calculation_rule()
commerce_product_pre_calculation_rule_keys in modules/product_pricing/commerce_product_pricing.module
Returns an array of rule keys used for pre-calculating product sell prices.
commerce_product_pre_calculation_settings_form in modules/product_pricing/includes/commerce_product_pricing_ui.admin.inc
Displays the settings form for managing product sell price pre-calculation.

File

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

Code

function commerce_product_valid_pre_calculation_rule($rule, $limit_validation = FALSE) {

  // If a rule configuration doesn't have any conditions, it doesn't need to
  // unique consideration in pre-calculation, as its actions will always apply.
  if (count($rule
    ->conditions()) == 0) {
    return FALSE;
  }

  // Inspect each condition on the rule configuration. This likely needs to be
  // recursive for conditions in nested operator groups.
  foreach ($rule
    ->conditions() as $condition) {

    // Look for line item usage in any selector in the condition settings.
    foreach ($condition->settings as $key => $value) {
      if (substr($key, -7) == ':select') {

        // If the selector references either line-item or line-item-unchanged,
        // the Rule is not valid for price pre-calculation.
        if (strpos($value, 'line-item') === 0) {
          return FALSE;
        }
      }
    }
  }

  // Allow other modules to invalidate this rule configuration.
  if (!$limit_validation) {
    if (in_array(FALSE, module_invoke_all('commerce_product_valid_pre_calculation_rule', $rule))) {
      return FALSE;
    }
  }
  return TRUE;
}