You are here

function commerce_shipping_line_item_add_form_submit in Commerce Shipping 7.2

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

Adds the selected shipping information to a new shipping line item.

Parameters

object $line_item: The newly created line item object.

array $element: The array representing the widget form element.

array $form_state: The present state of the form upon the latest submission.

array $form: The actual form array.

Return value

bool NULL if all is well or an error message if something goes wrong.

File

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

Code

function commerce_shipping_line_item_add_form_submit($line_item, $element, &$form_state, $form) {
  $order = $form_state['commerce_order'];

  // Ensure a quantity of 1.
  $line_item->quantity = 1;

  // Use a custom service and rate amount if specified.
  if ($element['actions']['shipping_service']['#value'] == 'manual') {
    $service = $element['actions']['custom_rate']['shipping_service']['#value'];
    $shipping_service = commerce_shipping_service_load($service);

    // Build the custom unit price array.
    $unit_price = array(
      'amount' => commerce_currency_decimal_to_amount($element['actions']['custom_rate']['amount']['#value'], $element['actions']['custom_rate']['currency_code']['#value']),
      'currency_code' => $element['actions']['custom_rate']['currency_code']['#value'],
      'data' => array(),
    );

    // Add a price component for the custom amount.
    $unit_price['data'] = commerce_price_component_add($unit_price, $shipping_service['price_component'], $unit_price, TRUE, FALSE);
  }
  else {

    // Otherwise use the values for the selected calculated service.
    $service = $element['actions']['shipping_service']['#value'];
    $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $element['actions']['shipping_rates']['#value'][$service]);
    $unit_price = $line_item_wrapper->commerce_unit_price
      ->value();
  }

  // Populate the line item with the appropriate data.
  commerce_shipping_line_item_populate($line_item, $service, $unit_price);
}