--- uc_coupon.module.orig	2009-01-26 18:33:31.000000000 -0700
+++ uc_coupon.module	2009-02-22 22:50:09.000000000 -0700
@@ -160,6 +160,12 @@ function uc_coupon_add_form($action, $ci
 
   $value->valid_until = array('year' => format_date($value->valid_until, 'custom', 'Y'), 'month' => format_date($value->valid_until, 'custom', 'n'), 'day' => format_date($value->valid_until, 'custom', 'j'));
 
+  $products = explode(',', $value->products, -1);
+  $default_products = array();
+  foreach ($products as $key => $pid){
+    $default_products[] = $pid;
+  }
+
   $form['name'] = array(
     '#type' => 'textfield',
     '#title' => t('Coupon name'),
@@ -229,10 +235,12 @@ function uc_coupon_add_form($action, $ci
 
   $form['product'] = array(
     '#type' => 'select',
+    '#multiple' => TRUE,  // previously false in d.o version
     '#title' => t('Product'),
     '#description' => t('Choosing a product will make this coupon valid only for this product.'),
     '#options' => uc_coupon_load_products(),
-    '#default_value' => $value->products,
+    '#default_value' => $default_products,
+    '#required' => TRUE,
   );
 
   $form['users'] = array(
@@ -288,14 +296,13 @@ function uc_coupon_add_form_submit($form
     }
   }
 
-  // Currently only supports a single product, but could be extended for multiple products
-  // later, ideally storing this data in a new table coupon_products or something.
-  // foreach ($form['product'] as $key => $nid) {
-  //   if (is_numeric($nid) && $key > 0 && $nid > 0) {
-  //     $products .= $nid .',';
-  //   }
-  // }
-  $products = $form['product'];
+  foreach ($form['product'] as $key => $nid) {
+    if (is_numeric($nid) && $key > 0 && $nid > 0) {
+      $products .= $nid .',';
+    }else{
+      $products = '';
+    }
+  }
 
   // If the forms coupon id is not set then we try to insert a new coupon
   if (!isset($form['cid'])) {
@@ -319,7 +326,6 @@ function uc_coupon_add_form_submit($form
   drupal_goto("admin/store/customers/coupon");
 }
 
-
 /**
  * Load all users in the system.
  *
@@ -450,6 +456,26 @@ function uc_coupon_validate($code, $orde
       return $coupon_result;
     }
 
+    //  CHECK PRODUCTS 
+    if ($coupon_data->products != '') {
+      $coupon_products = explode(',', $coupon_data->products, -1);
+      // compare coupon products to order products
+      foreach ($order->products as $o_key => $o_product){
+        $order_products[] = $o_product->nid;
+        foreach ($coupon_products as $c_key => $c_product){
+          if ($o_product->nid == $c_product){
+            $total += $o_product->qty * $o_product->price;
+          }
+        }
+      }
+  		if ($total <= 0) {
+  			$coupon_result->message .= '<li>'.t('This coupon don\'t apply to your products.').'</li>';							
+        return $coupon_result;
+  		}
+    }else{
+     $total = $coupon_data->value;
+    }
+  
     //  CHECK USER ID
     if ($coupon_data->users != '') {
       global $user;
@@ -481,29 +507,12 @@ function uc_coupon_validate($code, $orde
     $coupon_result->code = $coupon_data->code;
     $coupon_result->cid = $coupon_data->cid;
 
-    // If this coupon is just for a single product then calculate value here.
-    if ($coupon_data->products) {
-      foreach ($order->products as $key => $product) {
-        if ($product->nid == $coupon_data->products) {
-          $total = $product->qty * $product->price;
-          $qty = $product->qty;
-        }
-      }
-      if ($coupon_data->type == 'percentage') {
-        $coupon_result->amount = $total * $coupon_data->value / 100;
-      }
-      else if ($coupon_data->type = 'price') {
-        $coupon_result->amount = $qty * $coupon_data->value;
-      }
-    }
-    // Normal non product coupons.
-    else if ($coupon_data->type == 'percentage') {
+    if ($coupon_data->type == 'percentage') {
       $coupon_result->amount = $cart_total * $coupon_data->value / 100;
     }
-    else if ($coupon_data->type == 'price') {
-      $coupon_result->amount = $coupon_data->value;
+    else if ($coupon_data->type = 'price') {
+      $coupon_result->amount = ($coupon_data->value <= $total) ? $coupon_data->value : $total;
     }
-
   }
   else {
     // The coupon was not found, inactive, or is expired in the list of coupons
