You are here

function commerce_discount_set_existing_line_item_price in Commerce Discount 7

Updates the unit price of the discount line item matching the named discount.

Will be updated only first discount line item. Non-discount line items are ignored.

Parameters

EntityDrupalWrapper $order_wrapper: The wrapped order entity.

string $discount_name: The name of the discount being applied.

array $discount_price: The discount amount price array (amount, currency_code).

Return value

object|bool The modified line item or FALSE if not found.

2 calls to commerce_discount_set_existing_line_item_price()
commerce_discount_fixed_amount in ./commerce_discount.rules.inc
Rules action: Apply fixed amount discount.
commerce_discount_percentage in ./commerce_discount.rules.inc
Rules action: Apply percentage discount.

File

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

Code

function commerce_discount_set_existing_line_item_price(EntityDrupalWrapper $order_wrapper, $discount_name, $discount_price) {
  $modified_line_item = FALSE;
  foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
    if ($line_item_wrapper
      ->getBundle() == 'commerce_discount') {

      // Add the discount component price if the line item was originally
      // added by discount module.
      $line_item = $line_item_wrapper
        ->value();
      if (isset($line_item->data['discount_name']) && $line_item->data['discount_name'] == $discount_name) {
        commerce_discount_set_price_component($line_item_wrapper, $discount_name, $discount_price);
        $line_item_wrapper
          ->save();
        $modified_line_item = $line_item;
        break;
      }
    }
  }
  return $modified_line_item;
}