You are here

function uc_coupon_uc_cart_alter in Ubercart Discount Coupons 7.3

Same name and namespace in other branches
  1. 6 uc_coupon.module \uc_coupon_uc_cart_alter()
  2. 7.2 uc_coupon.module \uc_coupon_uc_cart_alter()

Implements hook_uc_cart_alter().

This is called every time the cart is rebuild (e.g. when products are added), so it's a good place to revalidate our session coupons. We also add a fake cart item (if configured to show in cart) for each coupon. These will be removed at checkout.

File

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

Code

function uc_coupon_uc_cart_alter(&$items) {

  // Validate all codes in the session against the cart contents.
  $order = new UcOrder();
  $order->products = $items;
  $order->data = array();
  $coupons = uc_coupon_session_validate($order);
  if (variable_get('uc_coupon_show_in_cart', TRUE) && !empty($coupons)) {

    // If there are some valid coupons, then add them to the cart (but only if
    // they have a non-zero value.
    foreach ($coupons as $code => $coupon) {
      if ($coupon->amount != 0) {
        $items[] = _uc_coupon_cart_item($coupon);
      }
    }
  }
}