Adding a line item to an order from a module I'm trying to create a gift wrapping module for Drupal commerce. I have created the checkout pane that has a select box for the user to choose if they want their order gift wrapped (and a field to select the giftwrap price on the configuration form). I've also created a giftwrap line item type. In the pane's base_checkout_form_submit() function I would like to create a giftwrap line item that is added to the order alongside the products. This is what I've got so far:

/**
 * Implements base_checkout_form_submit()
 */
function commerce_giftwrap_pane_checkout_form_submit($form, &amp;$form_state, $checkout_pane, $order) {

  $default_currency_code = commerce_default_currency();
  if ($balance = commerce_payment_order_balance($order)) {
    $default_currency_code = $balance['currency_code'];
  }

  // Create the new line item.
  $line_item = commerce_line_item_new('giftwrap', $order-&gt;order_id);

  $line_item-&gt;line_item_label = 'Gift Wrapping';
  $line_item-&gt;quantity = 1;
  $line_item-&gt;commerce_unit_price['amount'] = variable_get('commerce_giftwrap_price', '2.00');
  $line_item-&gt;commerce_unit_price['currency_code'] = $default_currency_code;

  commerce_line_item_save($line_item);  
}


I haven't wrapped it in an if statement yet, I wanted to get it working first. This code is creating a line item in the database however it isn't adding the line item to shopping cart contents view on the checkout review page. I've altered the shopping cart view to include product line items and my newly created giftwrap line items.

Any help on this would be greatly appreciated.
You need to add the line item to the order itself. You can do this pretty easily with the entity wrapper:

$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper-&gt;commerce_line_items[] = $line_item;
$order_wrapper-&gt;save();