Hi - I noticed on the checkout page if you fill out your details, apply the coupon and then hit submit - but there's an error (say you forgot to check the "my billing info is the same as my shipping info") the coupon is NOT automatically re-applied to the order!!? You have to enter the coupon again and click "Apply to order" again for the coupon to be re-applied.

I can see this generating a lot of complaints where people have errors on the form but do not notice that the coupon has dropped out of their order when the page is redrawn following submission.

Any way to fix this??

thanks!

Jason

Comments

budda’s picture

Also in 5.x and very annoying for customers :-(

longwave’s picture

Priority: Critical » Normal

This is some kind of weird interaction between Form API validate handlers and the way Ubercart processes checkout panes. I have been trying to find a workaround for this in uc_coupon itself but so far have not been successful - the problem is that first time a validation failure occurs, the relevant data is not supplied to uc_checkout_pane_coupon() so the coupon field does not get refilled correctly and neither does the order total preview get triggered to update. If the form is submitted for a second time, the coupon will stick - it's just the first validation failure that doesn't work as expected. I suspect other checkout pane modules also won't handle this correctly and this should somehow be fixed in Ubercart core.

budda’s picture

Could you temporarily store stuff in the users session during validation?

longwave’s picture

The problem is the functions seem to get called in the wrong order, op 'view' is called first to display the coupon pane (on the first page load here, we don't have the submitted coupon available), then op 'process' is called to save the submitted coupon, but op 'view' is not called again to show the now-processed coupon. This is why it works on the second page load - the op 'view' is using the results of op 'process' on the previous page load, so as long as the customer entered the same coupon again it will appear to have worked correctly. Perhaps a workaround could be done with hook_form_alter...

jasonabc’s picture

Assigned: jasonabc » Unassigned

Just wondering if any headway has been made on this...?

micheleannj’s picture

Subscribing.

Even if there was a way to have a little red highlighting and a "Please re-enter your coupon code" message just to draw attention to it...

Thanks,

entrigan’s picture

Hmmm, this is very problematic, maybe this can be solved with a hidden form field?

entrigan’s picture

Ok I got this working through an ugly hook_form_alter implement. Basically I added a pane called "coupon" with the value of the coupon. Then I would check if it was set, and if so I set the $_SESSION['uc_coupon'] = $form['panes']['coupon']['#value'].

Hopefully a clean solution will come through soon though!

BigTuna’s picture

Same problem (I think). Basically I have to "Apply to Order" twice. When I "Apply to Order" for the first time I get the message that my credit card information (from the payment pane) is missing even with the correct information in the billing information pane . If I re-enter the coupon and "Apply to Order" a second time the order total preview gets updated like it was suppose to do the first time (and the uc free order kicks in). Eager for a solution. Entrigan, would you be able to describe your solution in more detail?

Thanks.

If this isn't the same problem I'll create a new issue.

Edit: Re-reading post #2 from longwave... I'm definitely having the same issue.

BigTuna’s picture

Any progress on this one?

5t4rdu5t’s picture

I'm not sure if this is the best solution, but at least has solved the problem for me. I've just added the following line of code

  $_SESSION['uc_coupon'] = $coupon->code;

to the uc_coupon_checkout_apply function. So the modified function looks like this:

<?php 

/**
 * Checkout pane AJAX callback.
 **/
function uc_coupon_checkout_apply() {
  $coupon = uc_coupon_validate($_POST['code']);
  if ($coupon->valid) {
    $_SESSION['uc_coupon'] = $coupon->code;
    $coupon->message = t('The coupon has been applied to your order.');
  }

  drupal_set_header("Content-Type: text/javascript; charset=utf-8");
  print drupal_to_js($coupon);
  exit();
}

?>

I hope it helps someone else.

BigTuna’s picture

Unfortunately I had no luck with that solution. It still requires I "Apply to Order" twice before the "Order total preview:" is updated properly.

5t4rdu5t’s picture

That's strange.. we've tested it several times in different environments and worked just fine. Using uc_coupon 6.x-1.3 on Ubercart core 6.x-2.2 and Drupal core 6.16.

jasonabc’s picture

thanks for the code post mate - will check it out and see if it works for me.

BigTuna’s picture

Any luck jasonabc? I'm curious to know if it's worked for others.

I'm continuing to troubleshoot the problem on my end...

Edit: Upon further troubleshooting, it appears that the uc_coupon_checkout_apply() never gets called. On the second time I apply (or if I refresh after the first apply) it picks up the code from $arg1->data['coupon']; in the uc_checkout_pane_coupon($op, &$arg1, $arg2) function and validates and works. I'll continue to follow the process and hopefully find the solution.

Edit #2:

Re-reading longwave's post.. in a last desperate attempt I decided to try calling the function again with op "view" at the end of op "process". Somehow it seems to have worked.


function uc_checkout_pane_coupon($op, &$arg1, $arg2) {
  switch ($op) {
 
...
    case 'process':
      if ($arg2['code']) {
        $arg1->data['coupon'] = strtoupper(check_plain($arg2['code']));
        $coupon = uc_coupon_validate($arg1->data['coupon']);
        if (!$coupon->valid) {
          drupal_set_message($coupon->message, 'error');
          return FALSE;
        }
        _uc_coupon_apply_to_order($arg1->order_id, $coupon);
        uc_checkout_pane_coupon("view", &$arg1, $arg2);
      }
      return TRUE;

...

Maybe someone else can help explain if this is a valid solution.

jasonabc’s picture

hi guys - sorry for the delay here. I applied Libriana's fix and it worked for me. I'm running D6.14 and UC 2.2

cheers

Jason

5t4rdu5t’s picture

The fix on #11 seems to work perfectly for Windows and Linux, but is failing tests for Mac users.

luketsimmons’s picture

I applied both fixes (#11 and #15), which doesn't help to resolve the issue properly but I can say it works OK now and I'm Mac based, although I doubt that has much to do with it as the code changes are PHP.

I believe the reason #15 works is because it runs the switch with the view op, which adds the js and processes the $_SESSION stored coupon value, which we set the first time we applied the coupon code (thanks to the #11 fix).

The view op does some extra bits like generate the Apply to order button etc, but where you have called it we are not passing the form back at all, so it doesn't matter. Though this could be tidied up and perhaps an extra op used for pre or post validation may be?

Point is it seems to work now, so I'm happy :-)

Thanks,

Luke

longwave’s picture

Status: Active » Fixed

Hopefully, this should now be fixed in CVS. Please test and reopen if there are still issues.

pedrop’s picture

Version: 6.x-1.3 » 6.x-1.x-dev

We also noticed the same issue in 6.x-1.x-dev too.
After debugging the code I removed the unset($_SESSION['uc_coupon']); from function uc_coupon_order.
This seems to be working fine. What was the goal of that unset?

bkosborne’s picture

@pedrop, also removed it. Not sure why it was there. I also had to remove an unset in "view" for uc_checkout_pane_coupon. The purpose of THAT unset was to just get the coupon code from the cart pane if the user used it to add one.

Really, it should only be unset once an order is completed or a customer puts in a bad code.

longwave’s picture

Fix from #20 committed to CVS. The original intention here was to remove the coupon code once the order was complete, as it should be in the saved $order object and reloaded on the next page, but this isn't always the case if checkout validation fails. The coupon is removed from the session in hook_uc_checkout_complete() now, so this can be safely removed.

bkosborne’s picture

@longwave. I am glad I posted here. I had no idea about hook_uc_checkout_complete().... I was previously using hook_order and comparing statuses...

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

codefactory’s picture

if the code is preapplied before the checkout a solution is to comment out the code that unsets the variable from the SESSION uc_checkout_pane_coupon.

// Use coupon code from cart pane if available.
      if ($_SESSION['uc_coupon']) {
        $code = $_SESSION['uc_coupon'];
        //unset($_SESSION['uc_coupon']);
      }

This solution of course will leave the coupon as garbage in the SESSION but in my case I can live with this.