You are here

function uc_coupon_session_validate in Ubercart Discount Coupons 7.3

Same name and namespace in other branches
  1. 7.2 uc_coupon.module \uc_coupon_session_validate()

Validates all coupons in the current session. The validated coupons are statically cached for each request. The cache is rebuilt the first time this function is called, or every time the cart contents are rebuilt.

Parameters

$order: An order against which to validate the currently applied codes. If specified the cached list of valid coupons is rebuilt by revalidating all the codes in the session against that order.

Return value

An array of fully validated coupon objects, indexed by code.

7 calls to uc_coupon_session_validate()
uc_checkout_pane_coupon_automatic in ./uc_coupon.module
A checkout pane listing any automatic discounts.
uc_coupon_condition_order_has_coupon in ./uc_coupon.rules.inc
Check if an order has a coupon applied.
uc_coupon_form in ./uc_coupon.module
Form builder for the uc_coupon form.
uc_coupon_form_uc_cart_checkout_form_alter in ./uc_coupon.module
Implements hook_form_FORM_ID_alter() for uc_cart_checkout_form().
uc_coupon_uc_cart_alter in ./uc_coupon.module
Implements hook_uc_cart_alter().

... See full list

File

./uc_coupon.module, line 522
Provides discount codes and gift certificates for Ubercart.

Code

function uc_coupon_session_validate($order = NULL) {
  static $valids = NULL;

  // If a list of products is specified, then rebuild the list.
  if (isset($order)) {
    $valids = array();
    $order = clone $order;

    // We don't want to modify the order passed in.
    // Allow modules an opportunity to add or remove coupons from the session.
    module_invoke_all('uc_coupon_revalidate', $order);

    // Fetch all codes in the session.
    $session = uc_coupon_session_get();
    if (!empty($session)) {

      // Process all coupons in the session.
      global $user;
      $order->data['coupons'] = array();
      foreach ($session as $code => $op) {
        $coupon = uc_coupon_validate($code, $order, $user);
        if ($coupon->valid) {

          // Process valid coupons.
          $valids[$code] = $coupon;
          $order->data['coupons'][$code] = $coupon->discounts;
          switch ($op) {
            case 'submit':
            case 'retain':

              // For coupons which were not valid (new submissions) we notify user and modules.
              drupal_set_message($coupon->message);
              module_invoke_all('uc_coupon_apply', $coupon);

              // And we mark them for revalidation.
              uc_coupon_session_add($code, 'revalidate');
              break;
            case 'auto':

              // Automatic coupons are never added to the session.
              uc_coupon_session_clear($code);
              break;
          }
        }
        else {

          // Process invalid coupons.
          switch ($op) {
            case 'submit':

              // For new coupon submissions, just issue an error and remove from session.
              drupal_set_message($coupon->message, 'error');
              uc_coupon_session_clear($code);
              break;
            case 'revalidate':
              if (!empty($products)) {

                // Only issue a message if the cart is not empty.
                drupal_set_message(t('%title is no longer applicable to your order', array(
                  '%title' => $coupon->title,
                )));
              }
              module_invoke_all('uc_coupon_remove', $coupon);

              // Keep code in the session in case it becomes valid again.
              uc_coupon_session_add($code, 'retain');
              break;
            case 'auto':

              // Automatic coupons are never added to the session.
              uc_coupon_session_clear($code);
              break;
          }
        }
      }
    }
  }
  elseif (!isset($valids)) {
    uc_cart_get_contents();
  }
  return $valids;
}