Jump to:
| Project: | Ubercart Discount Coupons |
| Version: | 6.x-1.3 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
We have a fair number of orders created by staff for customers, and want to use coupons to provide standard discounts for these customers ( resellers, actually ). I've implemented the ability to apply a coupon directly to an order in the order edit form:
- and order / edit pane for the coupon input form
- a JS callback to apply the coupon to the order
- a copy of uc_coupon that takes the order_id as the second param, loads the order instead of the cart, then loops through the order->products array to calculate the total discount.
It occurred to me while doing this that a) this might be useful to somebody else, and b) this could be made simpler by re-factoring uc_coupon_validate() to take either the cart contents *or* the order products as a param.
I realize you're already thinking about version 2; if this feature is interesting enough to you I could roll a patch against uc_coupon 1.x HEAD in the next week or so.
Comments
#1
Version 2 is a long way off (or may never come, as uc_discount seems to be coming along well enough that it could eventually replace uc_coupon entirely), so if you have time to provide a patch for this, please do!
#2
I came up with the following code that works. I've put this in customtweaks.module which is my own custom module that I put on clients' sites for specific tweaks that they ask for. Replace 'customtweaks' with your own module name.
<?phpfunction customtweaks_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case "uc_order_view_update_form":
/** add coupon form **/
$form['coupon_fieldset'] = array(
'#type' => 'fieldset',
'#title' => 'Apply a Coupon',
'#collapsible' => true,
'#collapsed' => true,
);
$form['coupon_fieldset']['coupon'] = array(
'#type' => 'textfield',
'#title' => 'Coupon Code',
);
$form['#submit'][] = 'customtweaks_coupon_submit';
}
}
?>
And a bit further down:
<?phpfunction customtweaks_coupon_submit($form, &$form_state) {
$post = $form['order_comment_field']['order_comment']['#post'];
/** apply coupon to order **/
if ($post['coupon']) {
$coupon = uc_coupon_validate($post['coupon']);
if ($coupon->valid) {
_uc_coupon_apply_to_order($post['order_id'], $coupon);
}
}
}
?>
It doesn't warn you if the coupon is invalid and it might not be secure. I leave those to someone else to do as I'm on a tight schedule.
#3
subscribing
#4
subscribing
#5
I replaced coupon functionality of discount module with coupon module's code and it worked.
In uc_discount.module
/**
* Callback from hook_order_pane
*/
function uc_order_pane_coupon_discount($op, $arg1) {
switch ($op) {
case 'edit-form':
$pane = uc_checkout_pane_coupon_discount('view', $arg1, NULL);
$form['coupons'] = $pane['contents'];
unset($form['coupons']['coupon_code']['#title']);
$form['coupons']['add_coupon'] = array(
'#type' => 'submit',
'#value' => t('Apply coupon'),
);
return $form;
case 'edit-theme':
return drupal_render($arg1['coupons']);
case 'edit-ops':
return array(t('Apply coupon'));
case t('Apply coupon'):
if ($order = uc_order_load($arg1['order_id'])) {
$changes = array();
//$order->data['coupon_code'] = $arg1['coupon_code'];
$coupon = uc_coupon_validate($arg1['coupon_code'],$order);
if ($coupon->valid) {
_uc_coupon_apply_to_order($arg1['order_id'], $coupon);
$changes[] = t('Coupon code %code redeemed.', array('%code' => $arg1['coupon_code']));
}else{
drupal_set_message(t('Invalid coupon code1.'), 'error');
}
/*
if (uc_discount_validate_coupon($order)) {
$changes[] = t('Coupon code %code redeemed.', array('%code' => $order->data['coupon_code']));
}
else {
unset($order->data['coupon_code']);
drupal_set_message(t('Invalid coupon code.'), 'error');
}
*/
uc_order_save($order);
// Log any changes.
if (count($changes)) {
uc_order_log_changes($order->order_id, $changes);
}
}
break;
}
}