You are here

function commerce_coupon_action_create_coupon_line_item in Commerce Coupon 7

1 string reference to 'commerce_coupon_action_create_coupon_line_item'
commerce_coupon_rules_action_info in ./commerce_coupon.rules.inc
Implements hook_rules_action_info().

File

./commerce_coupon.rules.inc, line 201
Coupon rules integration file.

Code

function commerce_coupon_action_create_coupon_line_item($coupon, $commerce_order, $amount, $component_name, $currency_code) {
  if (!$commerce_order instanceof EntityMetadataWrapper) {
    $commerce_order = entity_metadata_wrapper('commerce_order', $commerce_order);
  }
  $line_item = commerce_coupon_line_item_new($coupon, $commerce_order->order_id
    ->raw());
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

  // Set the unit price on the line item object.
  $line_item_wrapper->commerce_unit_price->amount = $amount * -1;
  $line_item_wrapper->commerce_unit_price->currency_code = $currency_code;

  // Add the base price to the components array.
  if ($coupon->is_active && !commerce_price_component_load($line_item_wrapper->commerce_unit_price
    ->value(), $component_name)) {
    $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($line_item_wrapper->commerce_unit_price
      ->value(), $component_name, $line_item_wrapper->commerce_unit_price
      ->value(), TRUE, FALSE);
  }
  commerce_line_item_save($line_item);
  $commerce_order->commerce_line_items[] = $line_item;
  return array(
    'commerce_coupon_line_item' => $line_item,
  );
}