diff --git a/modules/line_item/includes/views/handlers/commerce_line_item_handler_field_edit_quantity.inc b/modules/line_item/includes/views/handlers/commerce_line_item_handler_field_edit_quantity.inc index e6b0d70..69e8187 100644 --- a/modules/line_item/includes/views/handlers/commerce_line_item_handler_field_edit_quantity.inc +++ b/modules/line_item/includes/views/handlers/commerce_line_item_handler_field_edit_quantity.inc @@ -74,6 +74,7 @@ class commerce_line_item_handler_field_edit_quantity extends views_handler_field function views_form_submit($form, &$form_state) { $field_name = $this->options['id']; + $line_item_updates = 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... @@ -81,19 +82,44 @@ class commerce_line_item_handler_field_edit_quantity extends views_handler_field // And the quantity on the form is different... if ($form_state['values'][$field_name][$row_id] != $line_item->quantity) { if ($form_state['values'][$field_name][$row_id] == 0) { - // If the quantity specified is 0, the line item is deleted. - $order = commerce_order_load($form_state['order']->order_id); - commerce_cart_order_product_line_item_delete($order, $line_item_id); + // If the quantity specified is 0, flag the line item for deletion. + $line_item_updates[] = array( + "action" => "delete", + "line_item_id" => $line_item_id + ); } else { - // Change the quantity and save the line item. - $line_item->quantity = $form_state['values'][$field_name][$row_id]; - - commerce_line_item_save($line_item); - entity_get_controller('commerce_line_item')->resetCache(array($line_item->line_item_id)); + // Flag the line item for update + $line_item_updates[] = array( + "action" => "update", + "line_item_id" => $line_item_id, + "old_value" => $line_item->quantity, + "new_value" => $form_state['values'][$field_name][$row_id] + ); } } } } + foreach ($line_item_updates as $line_item_update){ + // If the line item was flagged for deletion + if ($line_item_update['action'] == "delete"){ + // Delete the line item + $order = commerce_order_load($form_state['order']->order_id); + commerce_cart_order_product_line_item_delete($order, $line_item_update['line_item_id']); + } + // If the line item was flagged for update + else if ($line_item_update['action'] == "update"){ + // If the user submitted a new value + if ($line_item_update['old_value'] != $line_item_update['new_value']){ + // Load the line item + $line_item = commerce_line_item_load($line_item_update['line_item_id']); + + // Update the quantity and save + $line_item->quantity = $line_item_update['new_value']; + commerce_line_item_save($line_item); + entity_get_controller('commerce_line_item')->resetCache(array($line_item->line_item_id)); + } + } + } } }