Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jonathan_hunt’s picture

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

lathan’s picture

Status: Active » Needs work
FileSize
4.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.....

lathan’s picture

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" ]
        }
      }
    ]
  }
}
lathan’s picture

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

lathan’s picture

Status: Needs review » Needs work
FileSize
11.75 KB

More work on this patch

lathan’s picture

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

lathan’s picture

This patch now applies.

meghanmary’s picture

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?

lathan’s picture

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.

lathan’s picture

Status: Needs work » Needs review
FileSize
13.59 KB

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

meghanmary’s picture

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?

lathan’s picture

@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.

meghanmary’s picture

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.

lathan’s picture

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

pcambra’s picture

mrfelton’s picture

Status: Needs work » Needs review
FileSize
4.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.

lathan’s picture

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

pcambra’s picture

Status: Needs review » Needs work

This definitely needs a reroll after killing the log entity.

mitako’s picture

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

mitako’s picture

Problem solved my Calculate by percent rule was misconfigured

bkonetzny’s picture

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.

xenophyle’s picture

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().

xenophyle’s picture

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.)

essbee’s picture

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.

essbee’s picture

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.

Jayzen’s picture

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

pcambra’s picture

maciej.zgadzaj’s picture

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.

maciej.zgadzaj’s picture

Status: Needs work » Needs review
FileSize
4.92 KB

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

essbee’s picture

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.

pcambra’s picture

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.

maciej.zgadzaj’s picture

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.

maciej.zgadzaj’s picture

Status: Needs work » Needs review
maciej.zgadzaj’s picture

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).

pcambra’s picture

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.

maciej.zgadzaj’s picture

Status: Needs work » Needs review
FileSize
6.18 KB

Here, checkout pane settings option added.

pcambra’s picture

Status: Needs review » Needs work

We need to handle the variable deletion on module uninstall too

maciej.zgadzaj’s picture

Status: Needs work » Needs review
FileSize
6.68 KB

There you go.

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

lathan’s picture

just fixing white space issue.

maxime_sbk’s picture

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.

pcambra’s picture

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.

pcambra’s picture

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.

davident’s picture

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?

zroeduardo’s picture

Issue summary: View changes

I think this must be an issue, in: commerce_coupon_add_coupon_callback() on line 148:

$commands[] = ajax_command_replace('.view-commerce-cart-summary', $cart_summary);

The class was hard coded, so in my pane I've used another view to load the cart summary, making the class slightly different. I have to manually change the class to the loaded one for the refresh to work (in my case) :

$commands[] = ajax_command_replace('.view-checkout-cart-summary', $cart_summary);

skumar_drupal’s picture

Issue tags: +update commerce shipping (Flat rate shipping) when add coupon.

Hi,

I'm facing problem for update commerce shipping (Flat rate shipping) values in commerce coupon modules. I tried many patches but not found solution. Please guide me...