2

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, &$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->order_id);

  $line_item->line_item_label = 'Gift Wrapping';
  $line_item->quantity = 1;
  $line_item->commerce_unit_price['amount'] = variable_get('commerce_giftwrap_price', '2.00');
  $line_item->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.

4

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->commerce_line_items[] = $line_item;
$order_wrapper->save();
  • Yeah I think I was struggling to get my head around this entity_metadata_wrapper() business. It's sorted now and I've uploaded the module to a sandbox project. – splatio Dec 8 '11 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.