Posted by aaronbauman on October 27, 2009 at 3:55pm
Jump to:
| Project: | Ubercart Discount Coupons |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed (won't fix) |
Issue Summary
On a fresh install of uc_coupon, my site starts with no coupons defined.
There's no point in teasing the user to enter a coupon code if no entry could ever effect the order price.
Please hide -- or enable an admin toggle to optionally hide -- the coupon checkout form pane if no valid (unexpired, unused, undefined, etc) coupon codes exist.
Comments
#1
This option already exists in Ubercart at /admin/store/settings/checkout/edit/panes - uncheck the "Enabled" checkbox for the "Coupon discount" pane.
#2
You misunderstood the request.
The request is to hide the pane when there are no valid coupon codes, even when the "Coupon discount" pane is enabled.
#3
I'm also interested in this functionality.
#4
Here's how I've done this:
First you need a utility function to check if there are any valid coupons:
<?phpfunction _my_module_coupon_exists() {
$result = db_query('SELECT code FROM {uc_coupons} WHERE status = 1');
while ($row = db_fetch_object($result)) {
$code_check = uc_coupon_validate($row->code);
if ($code_check->valid) {
return TRUE;
}
}
return FALSE;
}
?>
Then, with this code in a form_alter(), use that function to decide if the uc_coupon pane should be hidden:
<?php
if (module_exists('uc_coupon') && $form_id == 'uc_cart_checkout_form') {
if (!_my_module_coupon_exists()) {
$form['panes']['coupon']['#access'] = FALSE;
}
}
?>
#5
Works like a charm, would be a nice addition to the module.
Thank you!
#6
The above suggestion won't check bulk coupons correctly. Also, if lots of coupons are set up, running this check could take a significant amount of time. I don't think this should be added to the module, but the above code can be used in a separate module if needed.
#7
I apologize, but my Drupal skills haven't quite got to such a level yet, but where is this code added? Is it added to template.php?
I'm afraid I haven't heard what a "utility function" is yet. Thanks for any help.
#8
We're using something very similar to #4. It would be nice to have available as an option.
Looking at how the code works, it would almost be possible to make a function that runs a SQL query and returns a list of coupons that *could* possibly be applied by the current user (if they knew the code), then an empty result would mean that the coupon box doesn't need to be shown. The problem blocking this approach is that the data on allowed roles and max uses per user has been stored in a serialized variable, and is therefore unable to be efficiently queried with a single SQL statement. That could be fixed with an update hook, and would make validating whether a coupon applies quite a bit more efficient to boot.
In the meantime I'll stick with iterating over all the codes, since we don't use bulk codes.
#9
Coupon validation is actually a fairly complicated process that depends not only on usage, but the order total price and quantity, the specific products in the order, taxonomy terms, and other data, many of which are serialized in the data array. Moving these to their own columns in the table would require a major refactoring of the code and result in a very large table. The query would still be inefficient for arrays (including allowed roles), which would still have to be deserialized, and some of the validation logic would still have to be performed outside of the query. If you wanted to take usage into account, you'd still have to query the usage (both overall and for the specific uid). Offhand, I can't think of an efficient way to do this in the general case, so I think it's better to let people handle specific, more limited cases (e.g. no bulk coupons, no product restrictions, etc.) as they arise.