You are here

function commerce_shipping_line_item_add_form in Commerce Shipping 7.2

Same name and namespace in other branches
  1. 7 commerce_shipping.module \commerce_shipping_line_item_add_form()

Helper function.

Returns the elements necessary to add a shipping line item through the line item manager widget.

File

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

Code

function commerce_shipping_line_item_add_form($form, &$form_state) {

  // Collect the available shipping rates for this order.
  $order = $form_state['commerce_order'];

  // If possible load the order, since it sometimes is in a stale state.
  if (!empty($order->order_id)) {
    $order = commerce_order_load($order->order_id);
  }
  commerce_shipping_collect_rates($order);

  // Store the available rates in the form.
  $form = array();
  $form['#attached']['css'][] = drupal_get_path('module', 'commerce_shipping') . '/theme/commerce_shipping.admin.css';
  $form['shipping_rates'] = array(
    '#type' => 'value',
    '#value' => $order->shipping_rates,
  );

  // Create an options array based on the rated services.
  $options = commerce_shipping_service_rate_options($order, $form_state);
  $options['manual'] = t('Manually specify a shipping service and rate.');
  $form['shipping_service'] = array(
    '#type' => 'radios',
    '#title' => t('Shipping service'),
    '#options' => $options,
    '#default_value' => key($options),
  );
  $form['custom_rate'] = array(
    '#type' => 'container',
    '#states' => array(
      'visible' => array(
        ':input[name="commerce_line_items[und][actions][shipping_service]"]' => array(
          'value' => 'manual',
        ),
      ),
    ),
  );
  $form['custom_rate']['shipping_service'] = array(
    '#type' => 'select',
    '#title' => t('Shipping service'),
    '#options' => commerce_shipping_service_options_list(),
  );
  $form['custom_rate']['amount'] = array(
    '#type' => 'textfield',
    '#title' => t('Shipping rate'),
    '#default_value' => '',
    '#size' => 10,
    '#prefix' => '<div class="commerce-shipping-rate">',
    '#states' => array(
      'visible' => array(
        ':input[name="commerce_line_items[und][actions][shipping_service]"]' => array(
          'value' => 'manual',
        ),
      ),
    ),
  );
  $form['custom_rate']['currency_code'] = array(
    '#type' => 'select',
    '#options' => commerce_currency_code_options_list(),
    '#default_value' => commerce_default_currency(),
    '#suffix' => '</div>',
  );
  return $form;
}