You are here

function commerce_shipping_clear_order in Commerce Shipping 7

Delete all shipping line items on an order.

Parameters

$order: The order object to delete the shipping line items from.

2 calls to commerce_shipping_clear_order()
commerce_shipping_pane_checkout_form_submit in includes/commerce_shipping.checkout_pane.inc
shipping pane: submit callback.
commerce_shipping_rules_remove_all in ./commerce_shipping.rules.inc
Rules action: Remove all shipping line items on an order.

File

./commerce_shipping.module, line 479
Defines the shipping system and checkout integration.

Code

function commerce_shipping_clear_order($order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

  // When deleting more than one line item, metadata_wrapper will give problems
  // if deleting while looping through the line items. So first remove from order
  // and then delete the line items.
  $line_items_to_delete = array();
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    if ($line_item_wrapper->type
      ->value() == 'shipping') {
      $line_items_to_delete[] = $line_item_wrapper->line_item_id
        ->value();
      $order_wrapper->commerce_line_items
        ->offsetUnset($delta);
    }
  }
  $order_wrapper
    ->save();

  // Delete line items.
  foreach ($line_items_to_delete as $line_item_id) {
    commerce_line_item_delete($line_item_id);
  }
}