You are here

function uc_coupon_session_validate in Ubercart Discount Coupons 7.2

Same name and namespace in other branches
  1. 7.3 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

$products: An array of products, in which case the cached list of valid coupons is rebuilt by revalidating all the codes in the session against that list.

NOTE: Forcing the cache to be rebuilt by calling this function with ($products != NULL) is discouraged. Instead, you should rebuild the current cart contents by calling uc_cart_get_contents(NULL, 'rebuild'). This will ensure that the valid coupons list and the shopping cart are in sync.

Return value

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

8 calls to uc_coupon_session_validate()
uc_checkout_pane_coupon in ./uc_coupon.module
Checkout Pane callback function.
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().

... See full list

File

./uc_coupon.module, line 515

Code

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

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

    // Allow modules an opportunity to add or remove coupons from the session.
    module_invoke_all('uc_coupon_revalidate', $products);

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

      // Process all coupons in the session.
      global $user;
      $order = new UcOrder();
      $order->products = $products;
      $order->data = array(
        '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;
}