You are here

function _commerce_discount_add_shipping_discount_price_component in Commerce Discount 7

Helper function for the shipping discount rules action.

Parameters

EntityDrupalWrapper $line_item_wrapper: The wrapped line item object to add the discount price component to.

EntityDrupalWrapper $order_wrapper: The wrapped order entity.

string $discount_name: The discount machine name.

integer $amount: The (negative) discount amount to add as price component.

Return value

bool A boolean indicating whether or not the discount price component has been added.

1 call to _commerce_discount_add_shipping_discount_price_component()
commerce_discount_shipping_service in ./commerce_discount.rules.inc
Rules action: Apply shipping discount.

File

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

Code

function _commerce_discount_add_shipping_discount_price_component($line_item_wrapper, $order_wrapper, $discount_name, $amount) {

  // Prevent the order total from going negative.
  $order_total = $order_wrapper->commerce_order_total
    ->value();
  if (-$amount > $order_total['amount']) {
    $amount = -$order_total['amount'];
  }
  if ($amount >= 0) {
    return FALSE;
  }
  $discount_price = array(
    'amount' => $amount,
    'currency_code' => $line_item_wrapper->commerce_unit_price->currency_code
      ->value(),
  );
  commerce_discount_add_price_component($line_item_wrapper, $discount_name, $discount_price);
  if ($line_item_wrapper
    ->getIdentifier() !== FALSE) {

    // Save the line item so that it correctly appears in the subsequent
    // order total calculation.
    $line_item_wrapper
      ->save();
    commerce_discount_calculate_order_total($order_wrapper);
    return TRUE;
  }
  return FALSE;
}