Index: uc_event_registration.module
===================================================================
--- uc_event_registration.module	(revision 2011)
+++ uc_event_registration.module	(working copy)
@@ -451,9 +451,23 @@
     // Fetch the product attributes from the webform submission because
     // uc_attribute components are in use.
     $attributes = _uc_event_registration_get_product_attributes($form, $form_state);
-
-    //add it to the cart
-    uc_cart_add_item($node->nid, $uc_event_quantity, array('attributes' => $attributes, 'uc_event_registration' => array('sid' => $sid_value)));
+    
+    // If we are editing an existing webform submission, update the associated cart item
+	  if (isset($form_state['values']['details']['is_new']) && $form_state['values']['details']['is_new'] == false) {
+			// Fetch and update the cart item based on submission ID
+	  	$cart_item = uc_event_registration_get_cart_item_from_sid ($sid_value);
+	  	if ($cart_item) {
+				$cart_item->qty = $uc_event_quantity;
+	  	  $cart_item->data['attributes'] = $attributes;  	  	  	  
+	  	  uc_cart_update_item($cart_item);  	  
+		    // Rebuild the cached cart items.
+    		uc_cart_get_contents(NULL, 'rebuild');	  		
+	  	}					
+	  }
+	  else {
+			//add it to the cart
+    	uc_cart_add_item($node->nid, $uc_event_quantity, array('attributes' => $attributes, 'uc_event_registration' => array('sid' => $sid_value)));		  	  		  		  	  	
+		}	
   }
 }//end submit function
 
@@ -940,3 +954,88 @@
     unset($node->attributes);
   }
 }
+
+/**
+ * Implements hook_form_FORM_ID_alter() for webform_submission_delete_form to add a submit handler which removes a paidevent
+ * product from the cart.  The new submit handler appears before the default handler so it runs first.
+ */
+function uc_event_registration_form_webform_submission_delete_form_alter (&$form, &$form_state) {
+ 	// Add submit handler BEFORE the existing Webform submit handler
+ 	$form['#submit'] = array_merge(array('uc_event_registration_webform_submission_delete_form_submit'), $form['#submit']);  
+}
+
+/**
+ * Webform submission delete form submit handler that removes the product in the cart
+ */
+function uc_event_registration_webform_submission_delete_form_submit($form, &$form_state) {	
+	$cart_item = uc_event_registration_get_cart_item_from_sid ($form_state['values']['details']['sid']);	
+	if ($cart_item) {
+		uc_cart_remove_item ($cart_item->nid, $cart_item->cart_id, $cart_item->data);	
+	}
+}
+
+/** 
+ * Implementation of hook_cart_item to remove a webform submission if a paidevent product is removed from the cart (and we are not in the process of creating a proper order)
+ */
+function uc_event_registration_cart_item($op, &$item) {
+	if ($op == 'remove') {
+		$data = unserialize($item->data);
+		// If there is an associated webform submission, delete it
+		if (isset($data['uc_event_registration']['sid'])) {
+			// Remove the webform submission if there are no order products associated with it (if there is an order product, we are probably emptying the cart after creating a proper order)
+			$order_products = uc_event_registration_get_order_products_from_sid($data['uc_event_registration']['sid']);			
+			if (empty($order_products)) {
+  			$node = node_load($item->nid);
+	  		if ($node) {
+	  			module_load_include('inc', 'webform', 'includes/webform.submissions');
+	  			$submission = webform_get_submission($item->nid, $data['uc_event_registration']['sid']);
+	  			webform_submission_delete($node, $submission);  				
+	  		}
+			}				
+		}			
+	}
+}
+
+/**
+ * Helper function to return cart items that are associated with a webform submission. 
+ * Returns a cart item or an array of cart items depending on the second parameter.
+ * @param $sid_value 
+ *   The webform submission sid
+ * @param $return_first_item_only 
+ *   TRUE - returns the first cart item that matches, FALSE - returns an array of cart items that match
+ */
+function uc_event_registration_get_cart_item_from_sid($sid_value, $return_first_item_only = true) {
+	$items = array();
+	foreach (uc_cart_get_contents() as $item) {
+		if (isset($item->data['uc_event_registration']['sid']) && $item->data['uc_event_registration']['sid'] == $sid_value) {
+			if ($return_first_item_only) {
+				return $item;
+			}
+			else {
+				$items[] = $item;	
+			}
+		}
+	}			
+	return $items;
+}
+
+/**
+ * Helper function to return the order products associated with a webform submission
+ * @param unknown_type $sid
+ */
+function uc_event_registration_get_order_products_from_sid ($sid) {
+    $sql = <<<SQL
+  SELECT *
+  FROM {uc_event_registration_submissions}
+  WHERE sid = %d
+  ORDER BY order_id DESC
+SQL;
+
+    $oids = array();
+    $res = db_query($sql, $sid);
+    while ($record = db_fetch_array($res)) {
+      $oids[] = $record;
+    }
+    
+    return $oids;
+}
\ No newline at end of file
