You are here

function commerce_shipping_pane_checkout_form_submit in Commerce Shipping 7

Same name and namespace in other branches
  1. 7.2 includes/commerce_shipping.checkout_pane.inc \commerce_shipping_pane_checkout_form_submit()

shipping pane: submit callback.

File

includes/commerce_shipping.checkout_pane.inc, line 135
Callback functions for the shipping module's checkout panes.

Code

function commerce_shipping_pane_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

  // In case this order already have shipping line items, we need to remove them.
  commerce_shipping_clear_order($order);
  $pane_form = $form[$checkout_pane['pane_id']];
  $pane_values = $form_state['values'][$checkout_pane['pane_id']];
  $class = $pane_form['commerce_shipping_plugin']['#value'];
  $rule_ids = explode('|', $pane_values['shipping_method']);
  $method_id = $rule_ids[0];

  // Only process if there were shipping methods available.
  if ($pane_values['shipping_methods']) {
    $order->data['shipping_method'] = $pane_values['shipping_method'];
    $default_currency_code = commerce_default_currency();
    if ($balance = commerce_payment_order_balance($order)) {
      $default_currency_code = $balance['currency_code'];
    }
    $form_values = isset($form_state['values']['commerce_shipping']['shipping_details']) ? $form_state['values']['commerce_shipping']['shipping_details'] : array();

    // Let the shipping method calculate the shipping price.
    $plugin = commerce_shipping_plugin_get_plugin('quotes', $method_id);
    $shipping_line_items = $class
      ->calculate_quote($default_currency_code, $form_values, $order, $form, $form_state);

    // Loop through the result and create line items if possible.
    if (is_array($shipping_line_items)) {
      foreach ($shipping_line_items as $shipping_line_item) {
        $line_item = commerce_shipping_line_item_new($plugin);
        $line_item->order_id = $order->order_id;
        $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
        if (is_numeric($shipping_line_item)) {
          $price = array(
            'amount' => $shipping_line_item,
          );
        }
        elseif (isset($shipping_line_item['amount'])) {
          $price = array(
            'amount' => $shipping_line_item['amount'],
            'currency_code' => !empty($shipping_line_item['currency_code']) ? $shipping_line_item['currency_code'] : $default_currency_code,
          );
        }
        elseif (isset($shipping_line_item['price'])) {
          $price = $shipping_line_item['price'];
        }
        if (isset($shipping_line_item['label'])) {
          $line_item_wrapper->line_item_label = $shipping_line_item['label'];
        }
        if (isset($shipping_line_item['quantity'])) {
          $line_item_wrapper->quantity = $shipping_line_item['quantity'];
        }

        // Require that the price is set.
        if (isset($price)) {

          // Add component if needed
          if (empty($price['data']['components'])) {
            $price_component = 'quote';
            if (!empty($plugin['price_component'])) {
              $price_component = 'quote_' . $plugin['name'];
            }
            $price['data'] = commerce_price_component_add($price, $price_component, $price, TRUE, FALSE);
          }

          // Make sure the currency code is set.
          if (empty($price['currency_code'])) {
            $price['currency_code'] = $default_currency_code;
          }
          $line_item_wrapper->commerce_unit_price = $price;
          rules_invoke_all('commerce_shipping_calculate', $line_item);
          $line_item_wrapper
            ->save();
          $order_wrapper->commerce_line_items[] = $line_item_wrapper
            ->value();
        }
      }
    }

    // Lastly we save the order.
    commerce_order_save($order);

    // This is not actually needed, but for flexibility allow shipping methods
    // to react on the form submission.
    $class
      ->shipping_items_created($pane_form['shipping_details'], $pane_values['shipping_details'], $order);
  }
}