You are here

function commerce_line_item_handler_field_edit_quantity::views_form_submit in Commerce Core 7

File

modules/line_item/includes/views/handlers/commerce_line_item_handler_field_edit_quantity.inc, line 85
Field handler to present a form field to change quantity of a line item. It's a dummy handler, most part of the implementation is done via post render hook.

Class

commerce_line_item_handler_field_edit_quantity
Field handler to present a field to change quantity of a line item.

Code

function views_form_submit($form, &$form_state) {
  $field_name = $this->options['id'];
  $deleted_line_items = array();
  $updated_line_items = array();
  foreach (element_children($form[$field_name]) as $row_id) {
    $line_item_id = $form[$field_name][$row_id]['#line_item_id'];

    // If the line item hasn't been deleted...
    if ($line_item = commerce_line_item_load($line_item_id)) {
      $form_quantity = $form_state['values'][$field_name][$row_id];

      // If the quantity on the form is different...
      if ($form_quantity != $line_item->quantity) {

        // If the quantity specified is 0, flag the line item for deletion.
        if ($form_quantity == 0) {
          $deleted_line_items[] = $line_item_id;
        }
        else {

          // Otherwise queue the line item quantity update.
          $updated_line_items[$line_item_id] = $form_quantity;
        }
      }
    }
  }

  // Process the deletes first.
  if (!empty($deleted_line_items)) {
    $order = commerce_order_load($form_state['order']->order_id);
    foreach ($deleted_line_items as $line_item_id) {
      $order = commerce_cart_order_product_line_item_delete($order, $line_item_id, TRUE);
    }
    commerce_order_save($order);
  }

  // Then process the quantity updates.
  foreach ($updated_line_items as $line_item_id => $quantity) {

    // Load the line item and update it.
    $line_item = commerce_line_item_load($line_item_id);
    $line_item->quantity = $quantity;
    commerce_line_item_save($line_item);
  }
}