You are here

function uc_coupon_submit in Ubercart Discount Coupons 6

Submit a coupon from checkout page or block.

uc_coupon_uc_cart_checkout_submit() uc_coupon_checkout_apply() uc_coupon_block_form_submit()

Parameters

$code : The user-entered code to submit.

$order : The current order object. If NULL, will use the current cart contents.

3 calls to uc_coupon_submit()
uc_coupon_block_form_submit in ./uc_coupon.module
Block form submit handler.
uc_coupon_checkout_apply in ./uc_coupon.module
Checkout pane AJAX callback.
uc_coupon_uc_cart_checkout_submit in ./uc_coupon.module
Submit handler for "Apply to Order" button in uc_cart_checkout. This handler is only invoked when JS is disabled.

File

./uc_coupon.module, line 1025
Provides discount coupons for Ubercart.

Code

function uc_coupon_submit($context, $code, $order = NULL) {
  $error = FALSE;
  $code = trim($code);
  if (empty($code)) {
    $coupon = new stdClass();
    $coupon->valid = FALSE;
    if (isset($_SESSION['uc_coupon'])) {
      $coupon->message = t('Coupon "@code" has been removed from your order.', array(
        '@code' => $_SESSION['uc_coupon'],
      ));
      unset($_SESSION['uc_coupon']);
    }
    else {
      $coupon->message = t('You must enter a valid coupon code.');
      $error = TRUE;
    }
  }
  else {
    $coupon = uc_coupon_validate($code, $order);
    if ($coupon->valid) {
      $_SESSION['uc_coupon'] = $code;
      if (!$coupon->message) {
        $amount = uc_coupon_price($coupon->amount);
        if ($coupon->amount) {
          if (variable_get('uc_coupon_show_in_cart', TRUE) || $context != 'block') {
            $coupon->message = t('A discount of !amount has been applied to your order.', array(
              '!amount' => $amount,
            ));
          }
          else {
            $coupon->message = t('A discount of !amount will be applied at checkout.', array(
              '!amount' => $amount,
            ));
          }
        }
        else {
          $coupon->message = t('Coupon "@code" has been applied to your order', array(
            '@code' => $code,
          ));
        }
      }
    }
    else {
      unset($_SESSION['uc_coupon']);
      $error = TRUE;
    }
  }

  // Return the coupon to JS for an ajax request; otherwise display a message.
  if ($context == 'ajax') {
    drupal_json($coupon);
    exit;
  }
  else {
    drupal_set_message($coupon->message, $error ? 'error' : 'status');
  }
}