You are here

function commerce_cart_form_commerce_order_ui_order_form_alter in Commerce Core 7

Implements hook_form_FORM_ID_alter().

Alter the order edit form so administrators cannot attempt to alter line item unit prices for orders still in a shopping cart status. On order load, the cart module refreshes these prices based on the current product price and pricing rules, so any alterations would not be persistent anyways.

See also

commerce_cart_commerce_order_load()

File

modules/cart/commerce_cart.module, line 438
Implements the shopping cart system and add to cart features.

Code

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

  // If the order being edited is in a shopping cart status and the form has the
  // commerce_line_items element present...
  if (commerce_cart_order_is_cart($order) && !empty($form['commerce_line_items'])) {

    // Grab the instance info for commerce_line_items and only alter the form if
    // it's using the line item manager widget.
    $instance = field_info_instance('commerce_order', 'commerce_line_items', field_extract_bundle('commerce_order', $order));
    if ($instance['widget']['type'] == 'commerce_line_item_manager') {

      // Loop over the line items on the form...
      foreach ($form['commerce_line_items'][$form['commerce_line_items']['#language']]['line_items'] as &$line_item) {

        // Disable the unit price amount and currency code fields.
        $language = $line_item['commerce_unit_price']['#language'];
        $line_item['commerce_unit_price'][$language][0]['amount']['#disabled'] = TRUE;
        $line_item['commerce_unit_price'][$language][0]['currency_code']['#disabled'] = TRUE;
      }
    }
  }
}