You are here

function commerce_gc_add_line_item in Commerce GC 7

Creates a giftcard use line item on the provided order.

Parameters

EntityDrupalWrapper $order_wrapper: The wrapped order entity.

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

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

1 call to commerce_gc_add_line_item()
commerce_gc_commerce_cart_order_refresh in ./commerce_gc.module

File

./commerce_gc.module, line 1020
Provides Giftcard coupon bundle, Giftcard Transaction entity and basic user interface elements.

Code

function commerce_gc_add_line_item(EntityDrupalWrapper $order_wrapper, EntityDrupalWrapper $coupon_wrapper, $price) {

  // Create a new line item.
  $line_item = entity_create('commerce_line_item', array(
    'type' => 'giftcard_use',
    'order_id' => $order_wrapper->order_id
      ->value(),
    'quantity' => 1,
  ));
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

  // Set a reference to the coupon
  $line_item_wrapper->commerce_giftcard = $coupon_wrapper
    ->value();

  // Set the giftcard line item price.
  commerce_gc_line_item_set_price($price, $line_item_wrapper, $coupon_wrapper);

  // Save the line item and add it to the order.
  $line_item_wrapper
    ->save();

  // The wrapper "set" pattern breaks down because of the way Discount module
  // rebases line items during order refresh, so we manipulate the entity
  // directly. See commerce_gc_commerce_cart_order_refresh() for a similar
  // pattern.
  $order = $order_wrapper
    ->value();
  $lang = field_language('commerce_order', $order, 'commerce_line_items');
  $order->commerce_line_items[$lang][] = array(
    'line_item_id' => $line_item->line_item_id,
  );
  return $line_item;
}