You are here

function commerce_discount_build_discount_rules in Commerce Discount 7

Build the rules configuration for the given discounts.

Parameters

array $discounts: An array of discount entities.

Return value

array An array of rules configuration objects.

2 calls to commerce_discount_build_discount_rules()
commerce_discount_default_rules_configuration in ./commerce_discount.rules_defaults.inc
Implements hook_default_rules_configuration().
_commerce_discount_rebuild_rules_config in ./commerce_discount.module
Actually rebuild the defaults of a given entity.

File

./commerce_discount.module, line 1334
Defines the discount and discount offer entities, bundles and functionality.

Code

function commerce_discount_build_discount_rules(array $discounts) {
  $rules = array();
  $types = commerce_discount_types();
  $offer_types = commerce_discount_offer_types();
  foreach ($discounts as $discount) {
    $wrapper = entity_metadata_wrapper('commerce_discount', $discount);
    $wrapper_properties = $wrapper
      ->getPropertyInfo();

    // Only for Commerce Discount wrappers with Commerce Discount Offer defined.
    if (isset($wrapper->commerce_discount_offer)) {
      $offer_bundle = $wrapper->commerce_discount_offer
        ->getBundle();
      if (!isset($offer_types[$offer_bundle])) {
        continue;
      }
      $type = $types[$discount->type];
      $offer_type = $offer_types[$offer_bundle];
      $rule = rules_reaction_rule();
      $rule->label = $discount->label;
      $rule->active = $discount->status;
      $rule->weight = !empty($discount->sort_order) ? $discount->sort_order - 11 : -1;
      $rule->tags = array(
        'Commerce Discount',
        check_plain($type['label']),
      );
      $rule
        ->event(!empty($offer_type['event']) ? $offer_type['event'] : $type['event'])
        ->action($offer_type['action'], array(
        'entity:select' => $type['entity type'],
        'commerce_discount' => $discount->name,
      ));

      // Add the compatibility condition to all discounts. Even if the current
      // discount is compatible with any other discount, we need to prevent it
      // from being added if a previously applied discount indicates it is not
      // compatible with the current one.
      if ($type['entity type'] == 'commerce_order') {
        $rule
          ->condition('commerce_discount_compatibility_check', array(
          'commerce_order:select' => 'commerce-order',
          'commerce_discount' => $discount->name,
        ));
      }
      else {
        $rule
          ->condition('commerce_discount_line_item_compatibility_check', array(
          'commerce_line_item:select' => 'commerce-line-item',
          'commerce_discount' => $discount->name,
        ));
      }

      // Let other modules alter the rule object, with configuration specific
      // to commerce discount. We don't invoke an alter function, as it can
      // be already achieved by implementing
      // hook_default_rules_configuration_alter().
      module_invoke_all('commerce_discount_rule_build', $rule, $discount);

      // Let inline_conditions fields add their own conditions.
      foreach ($wrapper_properties as $field_name => $property) {
        if (stripos($property['type'], 'inline_conditions') !== FALSE) {
          inline_conditions_build($rule, $wrapper->{$field_name}
            ->value());
        }
      }

      // Add the commerce discount to the rule configuration, so other may act
      // according to it, in hook_default_rules_configuration_alter().
      $rule->commerce_discount = $discount;
      $rule_machine_name = commerce_discount_build_rule_machine_name($discount->name);
      $rules[$rule_machine_name] = $rule;
    }
  }
  return $rules;
}