function uc_coupon_form_submit in Ubercart Discount Coupons 7.3
Same name and namespace in other branches
- 7.2 uc_coupon.module \uc_coupon_form_submit()
Submit handler for the uc_coupon form.
2 calls to uc_coupon_form_submit()
- uc_checkout_pane_coupon in ./
uc_coupon.module - Checkout Pane callback function.
- uc_coupon_order_submit in ./
uc_coupon.module
File
- ./
uc_coupon.module, line 1645 - Provides discount codes and gift certificates for Ubercart.
Code
function uc_coupon_form_submit($form, &$form_state) {
$trigger = $form_state['triggering_element']['#name'];
// Determine where the values are (they will be in a subarray if called from checkout or order page).
switch ($context = $form['#uc_coupon_form_context']) {
case 'checkout':
$values = $form_state['values']['panes']['coupon'];
break;
case 'order':
$values = $form_state['values']['coupon'];
break;
default:
$values = $form_state['values'];
}
// If this was the result of a 'remove' submission.
if (substr($trigger, 0, 16) == 'uc-coupon-remove') {
// See if there was an individual remove button clicked.
$code = substr($trigger, 17);
if (!empty($code)) {
if ($context == 'order') {
unset($form_state['order']->data['coupons']['code']);
}
else {
uc_coupon_session_clear($code);
drupal_set_message(t('Coupon "%code" has been removed from your order', array(
'%code' => $code,
)));
module_invoke_all('uc_coupon_remove', uc_coupon_find($code));
}
}
elseif (isset($values['coupons'])) {
$removed = array();
foreach ($values['coupons'] as $code => $selected) {
if (!$selected) {
$removed[] = $code;
if ($context == 'order') {
unset($form_state['order']->data['coupons'][$code]);
}
else {
uc_coupon_session_clear($code);
module_invoke_all('uc_coupon_remove', uc_coupon_find($code));
}
}
}
$n = count($removed);
if ($n > 1) {
$last = $removed[$n - 1];
$rest = implode(', ', array_slice($removed, 0, $n - 1));
drupal_set_message(t('Coupons %rest and %last have been removed from your order.', array(
'%rest' => $rest,
'%last' => $last,
)));
}
elseif (!empty($removed)) {
drupal_set_message(t('Coupon %code has been removed from your order', array(
'%code' => $removed[0],
)));
}
}
}
else {
$code = empty($values['code']) ? '' : strtoupper(trim($values['code']));
$removed = FALSE;
// If multiple codes are not enabled, then remove any codes currently applied.
if (!variable_get('uc_coupon_allow_multiple', FALSE) && count($session = uc_coupon_session_get()) > 0) {
if ($context == 'order') {
unset($form_state['order']->data['coupons']);
}
else {
foreach (array_keys($session) as $remove_code) {
uc_coupon_session_clear($remove_code);
drupal_set_message(t('Coupon "%code" has been removed from your order', array(
'%code' => $remove_code,
)));
module_invoke_all('uc_coupon_remove', uc_coupon_find($remove_code));
}
$removed = TRUE;
}
}
if (!empty($code)) {
if ($context == 'order') {
$coupon = uc_coupon_validate($code, $form_state['order'], user_load($form_state['order']->uid));
if ($coupon->valid) {
$form_state['order']->data['coupons'][$code] = $coupon->discounts;
}
else {
drupal_set_message($coupon->message, 'error');
}
}
else {
uc_coupon_session_add($code, 'submit');
}
}
elseif (!$removed) {
drupal_set_message(t("You must enter a valid coupon code."), 'error');
}
}
}