You are here

function commerce_shipping_delete_shipping_line_items in Commerce Shipping 7.2

Deletes all shipping line items on an order.

Parameters

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

bool $skip_save: Boolean indicating whether or not to skip saving the order in this function.

2 calls to commerce_shipping_delete_shipping_line_items()
commerce_shipping_pane_checkout_form_submit in includes/commerce_shipping.checkout_pane.inc
Checkout pane callback: submit the shipping checkout pane.
commerce_shipping_rate_apply in ./commerce_shipping.rules.inc
Action: Apply a shipping rate to an order.
1 string reference to 'commerce_shipping_delete_shipping_line_items'
commerce_shipping_default_rules_configuration in ./commerce_shipping.rules_defaults.inc
Implements hook_default_rules_configuration().

File

./commerce_shipping.module, line 1016
Defines a system for calculating shipping costs associated with an order.

Code

function commerce_shipping_delete_shipping_line_items($order, $skip_save = FALSE) {
  $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_item_ids = array();
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {

    // If this line item is a shipping line item...
    if ($line_item_wrapper
      ->getBundle() == 'shipping') {

      // Store its ID for later deletion and remove the reference from the line
      // item reference field.
      $line_item_ids[] = $line_item_wrapper->line_item_id
        ->value();
      $order_wrapper->commerce_line_items
        ->offsetUnset($delta);
    }
  }

  // If we found any shipping line items...
  if (!empty($line_item_ids)) {

    // First save the order to update the line item reference field value.
    if (!$skip_save) {
      commerce_order_save($order);
    }

    // Then delete the line items.
    commerce_line_item_delete_multiple($line_item_ids);
  }
}