You are here

function commerce_discount_fixed_amount in Commerce Discount 7

Rules action: Apply fixed amount discount.

1 string reference to 'commerce_discount_fixed_amount'
commerce_discount_commerce_discount_offer_type_info in ./commerce_discount.module
Implements hook_commerce_discount_offer_type_info().

File

./commerce_discount.rules.inc, line 878
Rules integration for the Commerce Discount module.

Code

function commerce_discount_fixed_amount(EntityDrupalWrapper $wrapper, $discount_name) {
  $discount_wrapper = entity_metadata_wrapper('commerce_discount', $discount_name);
  $discount_price = $discount_wrapper->commerce_discount_offer->commerce_fixed_amount
    ->value();
  $discount_price['amount'] = -$discount_price['amount'];

  // Get the line item types to apply the discount to.
  $line_item_types = variable_get('commerce_discount_line_item_types', array_diff(commerce_product_line_item_types(), array(
    'product_discount',
  )));
  switch ($wrapper
    ->type()) {
    case 'commerce_order':

      // Exit if the wrapper doesn't have a commerce_discounts property.
      if (!isset($wrapper->commerce_discounts)) {
        return;
      }

      // Set reference to the discount.
      // @todo: It doesn't work with the wrapper.
      $order = $wrapper
        ->value();

      // If the discount will bring the order to less than zero, set the
      // discount amount so that it stops at zero.
      $order_amount = $wrapper->commerce_order_total->amount
        ->value();
      if (-$discount_price['amount'] > $order_amount) {
        $discount_price['amount'] = -$order_amount;
      }
      $order->commerce_discounts[LANGUAGE_NONE][]['target_id'] = $discount_wrapper->discount_id
        ->value();

      // Modify the existing discount line item or add a new one if that fails.
      if (!commerce_discount_set_existing_line_item_price($wrapper, $discount_name, $discount_price)) {
        commerce_discount_add_line_item($wrapper, $discount_name, $discount_price);
      }

      // Update the total order price, for the next rules condition (if any).
      commerce_discount_calculate_order_total($wrapper);
      break;
    case 'commerce_line_item':

      // Check if the line item is configured in the settings to apply the
      // discount.
      if (!in_array($wrapper
        ->getBundle(), $line_item_types)) {
        return;
      }

      // Check whether this discount was already added as a price component.
      $unit_price = commerce_price_wrapper_value($wrapper, 'commerce_unit_price', TRUE);
      foreach ($unit_price['data']['components'] as $component) {
        if (!empty($component['price']['data']['discount_name']) && $component['price']['data']['discount_name'] == $discount_wrapper
          ->getIdentifier()) {
          return;
        }
      }

      // Do not allow negative line item totals.
      $line_item_amount = $wrapper->commerce_unit_price->amount
        ->value();
      if (-$discount_price['amount'] > $line_item_amount) {
        $discount_price['amount'] = -$line_item_amount;
      }
      commerce_discount_add_price_component($wrapper, $discount_name, $discount_price);
      break;
  }
}