You are here

function commerce_coupon_line_item_new in Commerce Coupon 7

Creates a new coupon line item populated with the proper coupon values.

Parameters

$commerce_coupon: The fully loaded coupon to add.

$order_id: Order to add this coupon.

Return value

Line item object with default values.

1 call to commerce_coupon_line_item_new()
commerce_coupon_action_create_coupon_line_item in ./commerce_coupon.rules.inc

File

./commerce_coupon.module, line 935
Coupon System for Drupal Commerce.

Code

function commerce_coupon_line_item_new($coupon, $order_id) {
  $coupon_wrapper = entity_metadata_wrapper('commerce_coupon', $coupon);

  // Create the new line item.
  $line_item = entity_create('commerce_line_item', array(
    'type' => 'commerce_coupon',
    'order_id' => $order_id,
    'quantity' => 1,
  ));
  $line_item->sku = $coupon->type . '_' . $coupon->coupon_id;
  $line_item->line_item_label = $coupon->type . ': ' . $coupon_wrapper->commerce_coupon_code
    ->value();
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $line_item_wrapper->commerce_coupon_reference = $coupon->coupon_id;

  // Return the line item.
  return $line_item_wrapper
    ->value();
}