I needed to allow my users the ability to change their webform submissions and have their cart contents update accordingly. As it has been mentioned by a couple of other people wanting this functionality, I've rolled a patch that does the following

  • if a webform submission is edited, the item in the cart is also modified (including changing the price if product attributes are used)
  • if a webform submission is deleted, the item in the cart is also deleted
  • if a cart item is deleted, the associated webform submission is also deleted
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

AndyF’s picture

Status: Needs review » Needs work

Thanks, sounds good. I'm currently working on a couple of other modules which will take priority, but I'll look at this when I can. Would you mind changing the white space to match the standards (looks like you're using tabs)?

Thanks

willowmedia’s picture

Status: Needs work » Needs review
FileSize
4.83 KB

Hi, thanks for taking the time to have a look - you're right there were tabs in there (I thought my editor automatically inserted spaces!).
Anyway, please find a new version of the patch attached with the tabs changed to 2 spaces.

AndyF’s picture

Status: Needs review » Needs work

Thanks. I've taken another look through, mostly minor formatting issues but hook_webform_submission_delete() is important imho.

+++ uc_event_registration.module	(working copy)
@@ -451,9 +451,23 @@
+    if (isset($form_state['values']['details']['is_new']) && $form_state['values']['details']['is_new'] == false) {

@@ -940,3 +954,88 @@
+function uc_event_registration_get_cart_item_from_sid($sid_value, $return_first_item_only = true) {

TRUE/FALSE should be uppercase.

+++ uc_event_registration.module	(working copy)
@@ -451,9 +451,23 @@
+    if (isset($form_state['values']['details']['is_new']) && $form_state['values']['details']['is_new'] == false) {

I don't think you need to use isset() here.

+++ uc_event_registration.module	(working copy)
@@ -940,3 +954,88 @@
+/**
+ * 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);  ¶
+  }

Could you use hook_webform_submission_delete() instead of a form submission handler? It's more robust.

+++ uc_event_registration.module	(working copy)
@@ -940,3 +954,88 @@
+    uc_cart_remove_item ($cart_item->nid, $cart_item->cart_id, $cart_item->data);  ¶

Shouldn't be a space after the function name.

+++ uc_event_registration.module	(working copy)
@@ -940,3 +954,88 @@
+ * 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)

Comments should always wrap at 80 characters.

+++ uc_event_registration.module	(working copy)
@@ -451,9 +451,23 @@
+    ¶
...
+    }  ¶

@@ -940,3 +954,88 @@
+   $form['#submit'] = array_merge(array('uc_event_registration_webform_submission_delete_form_submit'), $form['#submit']);  ¶
...
+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']);  ¶
...
+    uc_cart_remove_item ($cart_item->nid, $cart_item->cart_id, $cart_item->data);  ¶
...
+/** ¶
...
+      }        ¶
+    }      ¶
...
+ * Helper function to return cart items that are associated with a webform submission. ¶
...
+ * @param $sid_value ¶
...
+ * @param $return_first_item_only ¶
...
+    ¶

Strictly speaking, lines aren't supposed to end with white space (you can often tell the editor to remove this on save).

Thanks!

willowmedia’s picture

Status: Needs work » Needs review
FileSize
5.12 KB

Hi Andy,

I remember now that I originally tried implementing hook_webform_submission_delete() but was getting recursive errors! This time I've put a recursive checkflag in place to stop the error.

Please find attached a new patch.

Thanks
Nick.