As discussed in #1329308: Provide better core support for AJAX reloading of checkout panes / pages for Commerce and #1287124: Support recalculating shipping when the address is entered for Commerce Shipping, using ajax to reload the form would provide a better UI for customers. Added ajax functionality would greatly improve usability as customers would instantly see the effect of the added coupon.

Are there any current plans for this, and is it possible to do?

Comments

I'd like to see this also, along with AJAX validation of any coupon codes entered.

Status:Active» Needs work
StatusFileSize
new4.26 KB

Here is a patch that submits the coupons via an ajax add coupon button on the coupon form.

There is currently one issue I'm not sure how to get around, the view is using the log to get the value for the coupon. I assumed commerce_coupon_redeem_coupon($coupon, $order); would have handled... looks like it does not as the values for each coupon do not show up.....

Status:Needs work» Needs review

Patch works perfectly.... there are how ever in the latest dev branch missing actions....
namely.

{ "commerce_coupon_basic_fixed_amount" : {
    "LABEL" : "Calculate coupon with fixed amount",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_coupon" ],
    "ON" : [ "commerce_coupon_redeem" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "coupon" ], "field" : "commerce_coupon_fixed_amount" } },
      { "NOT AND" : [
          { "data_is_empty" : { "data" : [ "coupon:commerce-coupon-fixed-amount" ] } }
        ]
      },
      { "data_is" : {
          "data" : [ "coupon:commerce-coupon-fixed-amount:amount" ],
          "op" : "\u003E",
          "value" : 0
        }
      }
    ],
    "DO" : [
      { "commerce_coupon_action_create_coupon_line_item" : {
          "USING" : {
            "commerce_coupon" : [ "coupon" ],
            "commerce_order" : [ "commerce-order" ],
            "amount" : [ "coupon:commerce-coupon-fixed-amount:amount" ],
            "currency_code" : [ "coupon:commerce-coupon-fixed-amount:currency-code" ]
          },
          "PROVIDE" : { "commerce_coupon_line_item" : { "commerce_coupon_line_item" : "commerce coupon line item" } }
        }
      },
      { "commerce_coupon_action_set_granted_amount" : {
          "commerce_coupon_log" : [ "coupon-log" ],
          "amount" : [ "coupon:commerce-coupon-fixed-amount:amount" ],
          "currency_code" : [ "coupon:commerce-coupon-fixed-amount:currency-code" ]
        }
      }
    ]
  }
}

This rule wrong as not sure how get the outstanding amount (NOT THE ORDER TOTAL THATS WRONG) * by the percent amount.....?

{ "rules_calculate_coupon_with_percent_amount" : {
    "LABEL" : "Calculate coupon with percent amount",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "commerce_coupon" ],
    "ON" : [ "commerce_coupon_redeem" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "coupon" ], "field" : "commerce_coupon_percent_amount" } },
      { "NOT AND" : [
          { "data_is_empty" : { "data" : [ "coupon:commerce-coupon-percent-amount" ] } }
        ]
      },
      { "data_is" : {
          "data" : [ "coupon:commerce-coupon-percent-amount" ],
          "op" : "\u003E",
          "value" : "0"
        }
      }
    ],
    "DO" : [
      { "commerce_coupon_action_create_coupon_line_item" : {
          "USING" : {
            "commerce_coupon" : [ "coupon" ],
            "commerce_order" : [ "commerce-order" ],
            "amount" : [ "coupon:commerce-coupon-percent-amount" ],
            "currency_code" : [ "commerce-order:commerce-order-total:currency-code" ]
          },
          "PROVIDE" : { "commerce_coupon_line_item" : { "commerce_coupon_line_item" : "commerce coupon line item" } }
        }
      },
      { "commerce_coupon_action_set_granted_amount" : {
          "commerce_coupon_log" : [ "coupon-log" ],
          "amount" : [ "coupon:commerce-coupon-percent-amount" ],
          "currency_code" : [ "commerce-order:commerce-order-total:currency-code" ]
        }
      }
    ]
  }
}

StatusFileSize
new10.75 KB

Right figured it all out... here is working patch with ajax button and default actions.... for calculating the values.

Status:Needs review» Needs work
StatusFileSize
new11.75 KB

More work on this patch

StatusFileSize
new11.75 KB

Reroll to head of 7.x-1.x dev...

StatusFileSize
new11.75 KB

This patch now applies.

I would love to get this functionality working. Other than applying this patch is there anything else that needs to be done to get the button to show up and that new Calculate coupon with percent amount rule working?

StatusFileSize
new3.02 KB

shux man soz for all the patches there is a new file and my diff was just not including it right... rebuilt it from the ground up of dev.

@meghanmary

these are the issues still outstanding with this patch.
1. need to get % amount displaying correctly in the summary of the checkout page.. currently its not (this is a bug in this module but on my road map 2 fix)
2. the summary section of the form needs to update with the new total coupon discount value.

Status:Needs work» Needs review
StatusFileSize
new13.59 KB

- 'Ajaxified' form.
- Added default rules (fixed and percentage).
- Coupon table and Cart summary table are refreshed.

Status:Needs review» Needs work

Thanks for working on this - it is working great but I've come across one bug that is stumping me. If a user adds a coupon, does not hit "add coupon" but continues to enter the rest of the checkout information, and submits the checkout page the coupon gets validated at the time of checkout. That would not be a problem except that if the coupon is not valid the page returns with the "coupon is not valid" message but payment information still gets submitted. If a user then corrects the information and tries to submit the page nothing will work and the only way I have found of fixing this is to delete the user account and recreate. Any ideas on how to get around this issue?

@meghanmary nice find with that bug! I think that is a core coupon issue and that bug would work against the standard version with out this patch.. I will check it out in the morning as that is definitely not something we can allow...

Prob need a separate issue for that but ill link it to here once iv checked that out, thanks for the feedback.

Thanks jucallme. If you find anything out let me know, I'm wanting to use this for a live site but can't put it out there until this bug is resolved. I'll look into it also and update with anything I find.

@meghanmary Looks like here is the main issue http://drupal.org/node/1445464

Status:Needs work» Needs review
StatusFileSize
new4.29 KB

Updated version, seems to be working pretty well. If you have commerce_payment_example enabled, you'll need the patch from #1532550: Error in commerce_payment_example_submit_form can prevent ajax callbacks from firing on the checkout page. as without it, the ajax callback will not fire correctly.

Rerolled and fixed some issues.

- Fixed the pass by reference issue when loading log by order
- Checked the triggered key is set in the form

Status:Needs review» Needs work

This definitely needs a reroll after killing the log entity.

Hi,
I've installed last patch for adding an ajax button to coupon form and I don't know if I did something wrong but it is actually not working for percentage coupons.
Is there someone who is having the same issue ?
Can anybody help ?
Cheers

Problem solved my Calculate by percent rule was misconfigured

Still works, despite the removed log. Adding an coupon via this button assigns it to the order and reloads summary and coupon-listing, so this part seems fine.
One problem is that the field for the coupon stays filled after I added the coupon with this button. If i now submit the form, the coupon-code will get applied again. I think the field should get cleared, if the ajax-submit is successful.

One suggestion:

In function commerce_coupon_ajax_render_alter(), instead of assuming the view used is commerce_cart_summary,
do something like

list($view_id, $display_id) = explode('|', variable_get('commerce_cart_contents_pane_view', 'commerce_cart_summary|default'));

as done in commerce_cart_contents_pane_checkout_form().

When I apply the coupon code, the redeemed coupon list is updated, but the view for the cart summary (which contains the total) still contains the old data, as though the coupon was not applied. Has anyone seen anything like this? (This happens whether or not the code is modified as I suggested in the previous comment.)

This line
$form['commerce_coupon']['coupon_code']['#value'] = '';
does not work.

The coupon form doesnt exist in the $form variable at that point, so it isnt able to clear the coupon text field after it is applied.
This of course then cause an error to be thrown when the form is submitted as we dont want to allow the same coupon to be applied twice.

Well after chasing my tail for an afternoon with $form/$form_state and all manner of combinations, I'm no close to figuring out how to clear (and ideally disable in my case) the coupon code field after a successful "Add coupon"

Hopefully someone with better ajax-fu will be able to provide a solution.

I really need to add a button on my coupon, but I didn't success to apply the last patch with the last dev version of Commerce Coupon ...
It's an error with the new version or I'm stupid today ?

Thanks

Attached patch re-rolled against the latest dev:
- correctly clears the Coupon Code field after adding a new coupon,
- addresses the issue from comment #22.

I still don't like how it deals with errors (hiding already added coupons and showing Your coupon code is not valid error message instead, should fix it in the next patch version though.

Status:Needs work» Needs review
StatusFileSize
new4.92 KB

New version of the patch, properly showing errors and rebuilding the form only if necessary (when still staying on the same page).

Status:Needs review» Reviewed & tested by the community

Great patch.

Have tested and working in my implementation with both fixed and percentage based coupons.
Like the way it handles errors as it handles my rule to block multiple coupons perfectly (replaces the coupon on the order with the new one).

Great work.

Status:Reviewed & tested by the community» Needs work

Looks great, thanks all. Some minor details before pushing this in.

+++ b/includes/commerce_coupon.checkout_pane.inc
@@ -12,7 +12,11 @@
+    '#prefix' => '<div id="commerce-coupon-ajax-wrapper">',

I think that it's better to include some checkout references to the id class

+++ b/includes/commerce_coupon.checkout_pane.inc
@@ -20,6 +24,19 @@ function commerce_coupon_pane_checkout_form($form, &$form_state, $checkout_pane,
+    '#element_validate' => array(),

Do we need to clean up element validate for some reason?

+++ b/includes/commerce_coupon.checkout_pane.inc
@@ -32,6 +49,44 @@ function commerce_coupon_pane_checkout_form($form, &$form_state, $checkout_pane,
+          ¶

Watch out this space

+++ b/includes/commerce_coupon.checkout_pane.inc
@@ -32,6 +49,44 @@ function commerce_coupon_pane_checkout_form($form, &$form_state, $checkout_pane,
+            $coupon_current_order = &drupal_static('coupon_current_order');

If we're already adding the order id as static, probably it's enough with it and we don't need the extra boolean here.

I think that it's better to include some checkout references to the id class

Right you are sir. And speaking of which, I believe other panes' ids could use some unification too...

Do we need to clean up element validate for some reason?

Actually, no. I have just left it there from previous patches. Removed.

Watch out this space

Oh boy! Nasty Netbeans. ;)

If we're already adding the order id as static, probably it's enough with it and we don't need the extra boolean here.

It's not enough I'm afraid. Updating "Shopping cart contents" stops working after removing it.

Polished (sic!) patch attached.

Status:Needs work» Needs review

And another version, this one also properly updating shopping cart contents and added coupon list amounts for percentage coupons too (resolving the issue that mitako was talking about in comment #19).

Status:Needs review» Needs work

Looks great, thanks! I'd say that the only thing to do here is making this button optional (despite enabled would be the best option) and add a setting in the checkout pane for it so people that doesn't want multiple coupons at all can just disable it.

Status:Needs work» Needs review
StatusFileSize
new6.18 KB

Here, checkout pane settings option added.

Status:Needs review» Needs work

We need to handle the variable deletion on module uninstall too

Status:Needs work» Needs review
StatusFileSize
new6.68 KB

There you go.

And for all other module variables, which are not being deleted either, I have created a new issue. ;P

just fixing white space issue.

Category:feature» bug
Priority:Normal» Major

Hi guys,

I think I found a bug but I'm not sure as I don't know how it's suppose to work.
I moved the coupon pane in the checkout_review page (where you chose the payment methods). It make sense for me because
I'm using express checkout (without cart). When I select the payment and submit (continue), the checkout_review page is reloaded with the following error: Your coupon code is not valid.

I think there is an issue in the validation:

Line 58: if (!empty($code) && commerce_coupon_code_is_valid($code, $order)) {
No problem here, except in the else we are not checking for empty code so if we don't specify a code the validation is failing.

Am I right ?

UPDATE

No I'm not right. Sorry about that, I understand that this is the ajax validation so it make sense to fail on empty code.

Category:bug» feature
Priority:Major» Normal

Please don't modify the status of a feature request to "bug report" if the code hasn't been even committed. Also check the priority levels of issues

The problem that you described would seem as a limit validation errors issue but we're doing that in the patch, we need to test this in a single checkout page and see what happens.

Status:Needs review» Fixed

Great work in this issue to all that have participated!

Extra kudos to jucallme, mrfelton and maciej!

I've committed this with a slighlty modification, I've added the global validation variable to display the error message consistently with non ajax requests, basically this:

          if (isset($_commerce_coupon_validation_error_message)) {
            $message = $_commerce_coupon_validation_error_message;
          }
          else {
            $message = t('Your coupon code is not valid.');
          }
          form_set_error('commerce_coupon][coupon_code', $message);

For making the pane UI completely ok this should be next #1305242: Add a Remove coupon view button and Make a User used coupon rule condition

Status:Fixed» Closed (fixed)

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

Using #39 I've been able to spot an issue where if a coupon code is entered in the text field without clicking the add button, the order will have the coupon attached but without the discount applied. This happened when Payment pane was in the Checkout Pane and Review was in Completion pane.
Can anyone verify this?