Index: uc_discounts.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_discounts_alt/uc_discounts/uc_discounts.admin.inc,v
retrieving revision 1.10
diff -u -p -r1.10 uc_discounts.admin.inc
--- uc_discounts.admin.inc	22 Aug 2009 21:30:39 -0000	1.10
+++ uc_discounts.admin.inc	24 Sep 2009 22:52:01 -0000
@@ -1,5 +1,5 @@
 <?php
-//$Id: uc_discounts.admin.inc,v 1.10 2009/08/22 21:30:39 ryangroe Exp $
+//$Id: uc_discounts.admin.inc,v 1.9 2009/05/26 01:45:13 ryangroe Exp $
 
 
 /**
@@ -67,6 +67,7 @@ function uc_discounts_admin_settings() {
  * @see uc_discounts_form_submit()
  */
 function uc_discounts_form($form_state, $discount_id = 0) {
+
   $form           = array();
   $form_submitted = $form_state["submitted"];
   $is_edit        = $discount_id != 0;
@@ -106,24 +107,85 @@ function uc_discounts_form($form_state, 
     "#size" => 75,
   );
 
-  $form["qualifying_type"] = array(
+  $form["can_be_combined_with_other_discounts"] = array(
+    "#type" => "checkbox",
+    "#title" => t("Can be combined with other discounts"),
+    "#description" => t("Whether or not this discount will be applied if other discounts are."),
+    "#default_value" => isset($form_state["values"]["can_be_combined_with_other_discounts"])
+     ? $form_state["values"]["can_be_combined_with_other_discounts"] : CAN_BE_COMBINED_WITH_OTHER_DISCOUNTS_DEFAULT,
+  );
+
+  //Add custom weight range [-50, 50]
+  $options = array();
+  for ($i = -50; $i <= 50; $i++) $options[$i] = $i;
+  $form["weight"] = array(
     "#type" => "select",
-    "#title" => t("Qualifying type"),
-    "#description" => t("The amount used to determine if a cart qualifies for this discount."),
+    "#title" => t("Weight"),
+    "#description" => t("Lighter discounts are applied to an order first. This value is unimportant if there are no discounts on discount line items."),
+    "#options" => $options,
+    "#default_value" => isset($form_state["values"]["weight"]) ? $form_state["values"]["weight"] : 0,
+  );
+
+  $has_expiration = isset($form_state["values"]["has_expiration"]) ? $form_state["values"]["has_expiration"] : HAS_EXPIRATION_DEFAULT;
+  $display_string = ($has_expiration) ? "" : " style='display:none'";
+
+  $expiration_container_id = "expiration-container-". $seed;
+  $expiration_prefix = sprintf("<div id='%s' onclick='if ( jQuery(\"#%s input[type=\\\"checkbox\\\"]\").attr(\"checked\") {  )" . "{ jQuery(\"#%s .expiration-container\").slideDown(); }" . "else { jQuery(\"#%s .expiration-container\").slideUp();} }'>",
+    $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id
+  );
+
+  $form["expiration_header"] = array(
+    "#type" => "hidden",
+    "#prefix" => $expiration_prefix,
+  );
+
+  $form["has_expiration"] = array(
+    "#type" => "checkbox",
+    "#title" => t("Discount expires"),
+    "#description" => t("Whether or not this discount rule will expire on a given date."),
+    "#default_value" => $has_expiration,
+  );
+
+  $expiration = isset($form_state["values"]["expiration"]) ? $form_state["values"]["expiration"] : time();
+  $form["expiration"] = array(
+    "#type" => "date_popup",
+    "#date_type" => "DATE_UNIX",
+    "#title" => t("Discount expiration") . sprintf("<span title='". t("This field is required.") ."' class='form-required'>*</span>"),
+    "#description" => t("Date and time when the discount expires."),
+    "#default_value" => date("Y-m-d H:i:s", $expiration),
+    "#prefix" => sprintf("<div class='expiration-container'%s>", $display_string),
+    "#suffix" => "</div>",
+  );
+
+  $form["expiration_footer"] = array(
+    "#type" => "hidden",
+    "#suffix" => "</div>",
+  );
+
+  $form["qualifications"] = array(
+    "#type" => "fieldset",
+    "#title" => t("Conditions of Qualification"),
+    "#collapsible" => TRUE,
+    "#description" => t("Carts qualify for a discount based upon these conditions."),
+  );
+
+  $form["qualifications"]["qualifying_type"] = array(
+    "#type" => "select",
+    "#title" => t("Qualification type"),
+    "#description" => t("The type of qualification used to determine if a cart qualifies for this discount."),
     "#options" => qualifying_type_options(),
     "#default_value" => $form_state["values"]["qualifying_type"],
   );
 
-  $form["qualifying_amount"] = array(
+  $form["qualifications"]["qualifying_amount"] = array(
     "#type" => "textfield",
-    "#title" => t("Qualifying amount"),
-    "#description" => t("The amount required to qualify for the discount.	E.g. 50 (for $50), 5 (for 5 items).  Remember for a discount like 'buy 4 get 1 free' the qualifying amount is '5'."),
+    "#title" => t("Qualification amount"),
+    "#description" => t("The amount of qualification type required. E.g. 50 (for $50), 5 (for 5 items). Remember for a discount like 'buy 4 get 1 free' the qualifying amount is '5'."),
     "#default_value" => $form_state["values"]["qualifying_amount"],
     "#size" => 15,
     "#required" => TRUE,
   );
 
-
   $has_qualifying_amount_max = isset($form_state["values"]["has_qualifying_amount_max"]) ? $form_state["values"]["has_qualifying_amount_max"] : HAS_QUALIFYING_AMOUNT_MAX_DEFAULT;
   $display_string = ($has_qualifying_amount_max) ? "" : " style='display:none'";
 
@@ -134,12 +196,12 @@ function uc_discounts_form($form_state, 
     $qualifying_amount_max_container_id, $qualifying_amount_max_container_id
   );
 
-  $form["qualifying_amount_max_header"] = array(
+  $form["discount_set"]["qualifying_amount_max_header"] = array(
     "#type" => "hidden",
     "#prefix" => $qualifying_amount_max_prefix,
   );
 
-  $form["has_qualifying_amount_max"] = array(
+  $form["discount_set"]["has_qualifying_amount_max"] = array(
     "#type" => "checkbox",
     "#title" => t("Has max qualifying amount."),
     "#description" => t("Whether or not discount contains maximum qualifying amount."),
@@ -150,7 +212,7 @@ function uc_discounts_form($form_state, 
   $qualifying_amount_max = $form_state["values"]["role_ids"];
 
   //Create qualifying_amount_max form element
-  $form["qualifying_amount_max"] = array(
+  $form["discount_set"]["qualifying_amount_max"] = array(
     "#type" => "textfield",
     "#title" => t("Maximum qualifying amount"),
     "#description" => t("Maximum amount to NOT exceed to qualify for the discount.  E.g. 50 (for $50), 5 (for 5 items)."),
@@ -161,13 +223,19 @@ function uc_discounts_form($form_state, 
     "#suffix" => "</div>",
   );
 
-  $form["qualifying_amount_max_footer"] = array(
+  $form["discount_set"]["qualifying_amount_max_footer"] = array(
     "#type" => "hidden",
     "#suffix" => "</div>",
   );
 
+  $form["discount_set"] = array(
+    "#type" => "fieldset",
+    "#title" => t("Discount Application"),
+    "#description" => t("Details of the discount to apply"),
+    "#collapsible" => TRUE,
+  );
 
-  $form["discount_type"] = array(
+  $form["discount_set"]["discount_type"] = array(
     "#type" => "select",
     "#title" => t("Discount type"),
     "#description" => t("Type of discount to apply."),
@@ -175,7 +243,7 @@ function uc_discounts_form($form_state, 
     "#default_value" => $form_state["values"]["discount_type"],
   );
 
-  $form["discount_amount"] = array(
+  $form["discount_set"]["discount_amount"] = array(
     "#type" => "textfield",
     "#title" => t("Discount amount"),
     "#description" => t("The amount of discount.	E.g. 50 (for $50), 5 (for 5 items), or 0.05 (for 5%)"),
@@ -192,14 +260,14 @@ function uc_discounts_form($form_state, 
     $codes_container_id, $codes_container_id, $codes_container_id, $codes_container_id, $codes_container_id, $codes_container_id
   );
 
-  $form["codes_header"] = array(
+  $form["qualifications"]["codes_header"] = array(
     "#type" => "hidden",
     "#prefix" => $codes_prefix,
   );
 
-  $form["requires_code"] = array(
+  $form["qualifications"]["requires_code"] = array(
     "#type" => "checkbox",
-    "#title" => t("Require(s) code to activate discount."),
+    "#title" => t("Require code to activate discount."),
     "#default_value" => $requires_code,
   );
 
@@ -216,7 +284,7 @@ function uc_discounts_form($form_state, 
     $codes_string = create_codes_string($codes);
   }
 
-  $form["codes"] = array(
+  $form["qualifications"]["codes"] = array(
     "#type" => "textarea",
     "#title" => t("Discount codes") . sprintf("<span title='". t("This field is required.") ."' class='form-required'>*</span>"),
     "#description" => t("Enter discount codes in box above, one code per line.  Spaces are permitted but may confuse consumers. Note codes will be trimmed."),
@@ -226,7 +294,7 @@ function uc_discounts_form($form_state, 
     "#suffix" => "</div></div>",
   );
 
-  $form["codes_footer"] = array(
+  $form["qualifications"]["codes_footer"] = array(
     "#type" => "hidden",
     "#suffix" => "</div>",
   );
@@ -235,17 +303,19 @@ function uc_discounts_form($form_state, 
   $products_display_string = ($filter_type == FILTER_TYPE_PRODUCTS) ? "" : " style='display:none'";
   $terms_display_string = ($filter_type == FILTER_TYPE_TERMS) ? "" : " style='display:none'";
   $skus_display_string = ($filter_type == FILTER_TYPE_SKUS) ? "" : " style='display:none'";
+  $classes_display_string = ($filter_type == FILTER_TYPE_CLASS) ? "" : " style='display:none'";
 
   $filter_type_container_id = "filter-type-container-". $seed;
   $filter_type_prefix = sprintf(
-    "<div id='%s' onclick='value = jQuery(\"#%s select\").val();" . "if (value == %d) { jQuery(\"#%s .products-container\").slideDown(); jQuery(\"#%s .terms-container\").slideUp(); jQuery(\"#%s .skus-container\").slideUp(); }" . "if (value == %d) { jQuery(\"#%s .products-container\").slideUp(); jQuery(\"#%s .terms-container\").slideDown(); jQuery(\"#%s .skus-container\").slideUp(); }" . "if (value == %d) { jQuery(\"#%s .products-container\").slideUp(); jQuery(\"#%s .terms-container\").slideUp(); jQuery(\"#%s .skus-container\").slideDown(); }'>",
+    "<div id='%s' onclick='value = jQuery(\"#%s select\").val();" . "if (value == %d) { jQuery(\"#%s .products-container\").slideDown(); jQuery(\"#%s .terms-container\").slideUp(); jQuery(\"#%s .skus-container\").slideUp(); jQuery(\"#%s .classes-container\").slideUp(); }" . "if (value == %d) { jQuery(\"#%s .products-container\").slideUp(); jQuery(\"#%s .terms-container\").slideDown(); jQuery(\"#%s .skus-container\").slideUp(); jQuery(\"#%s .classes-container\").slideUp(); }" . "if (value == %d) { jQuery(\"#%s .products-container\").slideUp(); jQuery(\"#%s .terms-container\").slideUp(); jQuery(\"#%s .skus-container\").slideDown(); jQuery(\"#%s .classes-container\").slideUp(); }" . "if (value == %d) { jQuery(\"#%s .products-container\").slideUp(); jQuery(\"#%s .terms-container\").slideUp(); jQuery(\"#%s .skus-container\").slideUp(); jQuery(\"#%s .classes-container\").slideDown(); }'>",
     $filter_type_container_id, $filter_type_container_id,
-    FILTER_TYPE_PRODUCTS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id,
-    FILTER_TYPE_TERMS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id,
-    FILTER_TYPE_SKUS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id
+    FILTER_TYPE_PRODUCTS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id,
+    FILTER_TYPE_TERMS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id,
+    FILTER_TYPE_SKUS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id,
+    FILTER_TYPE_CLASS, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id, $filter_type_container_id
   );
 
-  $form["filter_type_header"] = array(
+  $form["discount_set"]["filter_type_header"] = array(
     "#type" => "hidden",
     "#prefix" => $filter_type_prefix,
   );
@@ -255,10 +325,11 @@ function uc_discounts_form($form_state, 
   $options[FILTER_TYPE_PRODUCTS] = t("Filter By Products");
   $options[FILTER_TYPE_TERMS] = t("Filter By Terms");
   $options[FILTER_TYPE_SKUS] = t("Filter By SKUs");
-  $form["filter_type"] = array(
+  $options[FILTER_TYPE_CLASS] = t("Filter by Class");
+  $form["discount_set"]["filter_type"] = array(
     "#type" => "select",
     "#title" => t("Filter Type"),
-    "#description" => t("Determine whether to filter by products, terms or SKUs."),
+    "#description" => t("Determine whether to filter by products, terms, SKUs, or class. Discount is applied to only these products."),
     "#options" => $options,
     "#default_value" => $filter_type,
     "#required" => TRUE,
@@ -277,7 +348,7 @@ function uc_discounts_form($form_state, 
   );
   $options[ALL_PRODUCTS] = t("<All Products>");
   while ($row = db_fetch_object($result)) $options[$row->nid] = $row->title ." (". $row->model .")";
-  $form["product_ids"] = array(
+  $form["discount_set"]["product_ids"] = array(
     "#type" => "select",
     "#title" => t("Products") . sprintf("<span title='". t("This field is required.") ."' class='form-required'>*</span>"),
     "#description" => t("Select all products this discount applies to or &lt;All Products&gt; to apply to all products."),
@@ -300,7 +371,7 @@ function uc_discounts_form($form_state, 
   $result             = db_query("SELECT tid, name FROM {term_data} ORDER BY weight");
   $options[ALL_TERMS] = t("<All Terms>");
   while ($row = db_fetch_object($result)) $options[$row->tid] = $row->name;
-  $form["term_ids"] = array(
+  $form["discount_set"]["term_ids"] = array(
     "#type" => "select",
     "#title" => t("Terms") . sprintf("<span title='". t("This field is required.") ."' class='form-required'>*</span>"),
     "#description" => t("Select all terms this discount applies to or &lt;All Terms&gt; to apply to all terms.  Selections are <b>not</b> recursive."),
@@ -323,7 +394,7 @@ function uc_discounts_form($form_state, 
   $result            = db_query("SELECT p.model, n.title FROM {uc_products} p, {node} n WHERE p.nid=n.nid ORDER BY p.model");
   $options[ALL_SKUS] = t("<All SKUs>");
   while ($row = db_fetch_object($result)) $options[$row->model] = $row->model ." (". $row->title .")";
-  $form["skus"] = array(
+  $form["discount_set"]["skus"] = array(
     "#type" => "select",
     "#title" => t("SKUs") . sprintf("<span title='". t("This field is required.") ."' class='form-required'>*</span>"),
     "#description" => t("Select all SKUs this discount applies to or &lt;All SKUs&gt; to apply to all SKUs.  Selections are <b>not</b> recursive."),
@@ -334,7 +405,30 @@ function uc_discounts_form($form_state, 
     "#suffix" => "</div>",
   );
 
-  $form["filter_type_footer"] = array(
+  //Get current discount product classes
+  $classes = null;
+  if (isset($form_state["values"]["classes"])) {
+    $classes = $form_state["values"]["classes"];
+  }
+  elseif ($is_edit) {  $classes = get_classes_for_discount($discount_id, FALSE);}
+
+  //Create Classes form element
+  $options = array();
+  $result = db_query("SELECT pc.pcid, pc.name FROM {uc_product_classes} pc ORDER BY pc.pcid");
+  $options[ALL_CLASSES] = t("<All Product Classes>");
+  while ($row = db_fetch_object($result)) $options[$row->pcid] = $row->pcid ." (". $row->name .")";
+  $form["discount_set"]["classes"] = array(
+    "#type" => "select",
+    "#title" => t("Classes") . sprintf("<span title='". t("This field is required.") ."' class='form-required'>*</span>"),
+    "#description" => t("Select all product classes that this discount applies to or &lt;All Classes&gt; to apply to all classes.  Selections are <b>not</b> recursive."),
+    "#options" => $options,
+    "#default_value" => $classes,
+    "#multiple" => TRUE,
+    "#prefix" => sprintf("<div class='classes-container'%s", $classes_display_string),
+    "#suffix" => "</div>",
+  );
+
+  $form["discount_set"]["filter_type_footer"] = array(
     "#type" => "hidden",
     "#suffix" => "</div>",
   );
@@ -347,14 +441,14 @@ function uc_discounts_form($form_state, 
     $roles_container_id, $roles_container_id, $roles_container_id, $roles_container_id, $roles_container_id, $roles_container_id
   );
 
-  $form["roles_header"] = array(
+  $form["qualifications"]["roles_header"] = array(
     "#type" => "hidden",
     "#prefix" => $roles_prefix,
   );
 
-  $form["has_role_filter"] = array(
+  $form["qualifications"]["has_role_filter"] = array(
     "#type" => "checkbox",
-    "#title" => t("Filter based on roles."),
+    "#title" => t("Qualification by Role(s)."),
     "#default_value" => $has_role_filter,
   );
 
@@ -371,10 +465,10 @@ function uc_discounts_form($form_state, 
   $result = db_query("SELECT rid, name FROM {role} ORDER BY rid");
   $options[ALL_ROLES] = t("<All Roles>");
   while ($row = db_fetch_object($result)) $options[$row->rid] = $row->name;
-  $form["role_ids"] = array(
+  $form["qualifications"]["role_ids"] = array(
     "#type" => "select",
     "#title" => t("Roles") . sprintf("<span title='". t("This field is required.") ."' class='form-required'>*</span>"),
-    "#description" => t("Select all roles this discount applies to or &lt;All Roles&gt; to apply to all roles."),
+    "#description" => t("Select all roles that this discount applies to or &lt;All Roles&gt; to apply to all roles."),
     "#options" => $options,
     "#default_value" => $role_ids,
     "#multiple" => TRUE,
@@ -382,19 +476,34 @@ function uc_discounts_form($form_state, 
     "#suffix" => "</div>",
   );
 
-  $form["roles_footer"] = array(
-    "#type" => "hidden",
-    "#suffix" => "</div>",
+  $form["qualifications"]["roles_footer"] = array(
+    "#type" => "markup",
+    "#value" => "</div>",
   );
 
-  $form["requires_single_product_to_qualify"] = array(
+  $form["qualifications"]["requires_single_product_to_qualify"] = array(
     "#type" => "checkbox",
     "#title" => t("Require single product to qualify."),
     "#description" => t("Requires a single product to meet the qualifying requirements to receive discount.	Otherwise qualifying requirements may be met by the combination of selected products."),
     "#default_value" => isset($form_state["values"]["requires_single_product_to_qualify"]) ? $form_state["values"]["requires_single_product_to_qualify"] : REQUIRES_SINGLE_PRODUCT_TO_QUALIFY_DEFAULT,
   );
 
-  $form["max_times_applied"] = array(
+  //Create SKUs form element
+  $options          = array();
+  $result           = db_query("SELECT p.model, n.title FROM {uc_products} p, {node} n WHERE p.nid=n.nid ORDER BY p.model");
+  $options[NO_SKUS] = t("Select a single SKU...");
+  while ($row = db_fetch_object($result)) $options[$row->model] = $row->model ." (". $row->title .")";
+
+  $form["qualifications"]["required_product"] = array(
+    "#type" => "select",
+    "#title" => t("Required product"),
+    "#description" => t("Select a single product that the cart must contain."),
+    "#options" => $options,
+    "#default_value" => $form_state["values"]["required_product"],
+  );
+
+
+  $form["discount_set"]["max_times_applied"] = array(
     "#type" => "textfield",
     "#title" => t("Maximum times applied"),
     "#description" => t("Number of times this discount can be applied to a single cart (0 for unlimited).  Does not apply to 'Percent off' discounts."),
@@ -404,15 +513,7 @@ function uc_discounts_form($form_state, 
     "#required" => TRUE,
   );
 
-  $form["can_be_combined_with_other_discounts"] = array(
-    "#type" => "checkbox",
-    "#title" => t("Can be combined with other discounts"),
-    "#description" => t("Whether or not this discount will be applied if other discounts are."),
-    "#default_value" => isset($form_state["values"]["can_be_combined_with_other_discounts"])
-     ? $form_state["values"]["can_be_combined_with_other_discounts"] : CAN_BE_COMBINED_WITH_OTHER_DISCOUNTS_DEFAULT,
-  );
-
-  $form["max_uses"] = array(
+  $form["discount_set"]["max_uses"] = array(
     "#type" => "textfield",
     "#title" => t("Max uses"),
     "#description" => t("Number of times this discount can be applied (0 for unlimited)."),
@@ -422,7 +523,7 @@ function uc_discounts_form($form_state, 
     "#required" => TRUE,
   );
 
-  $form["max_uses_per_user"] = array(
+  $form["discount_set"]["max_uses_per_user"] = array(
     "#type" => "textfield",
     "#title" => t("Max uses per user"),
     "#description" => t("Number of times this discount can be applied to a particular user (0 for unlimited)."),
@@ -432,7 +533,7 @@ function uc_discounts_form($form_state, 
     "#required" => TRUE,
   );
 
-  $form["max_uses_per_code"] = array(
+  $form["discount_set"]["max_uses_per_code"] = array(
     "#type" => "textfield",
     "#title" => t("Max uses per code"),
     "#description" => t("Number of times this discount can be applied to a particular code (0 for unlimited).  Note: if 'Max uses' (if set) still applies as overall maximum number of uses for this discount."),
@@ -442,42 +543,6 @@ function uc_discounts_form($form_state, 
     "#required" => TRUE,
   );
 
-  $has_expiration = isset($form_state["values"]["has_expiration"]) ? $form_state["values"]["has_expiration"] : HAS_EXPIRATION_DEFAULT;
-  $display_string = ($has_expiration) ? "" : " style='display:none'";
-
-  $expiration_container_id = "expiration-container-". $seed;
-  $expiration_prefix = sprintf("<div id='%s' onclick='if ( jQuery(\"#%s input[type=\\\"checkbox\\\"]\").attr(\"checked\") {  )" . "{ jQuery(\"#%s .expiration-container\").slideDown(); }" . "else { jQuery(\"#%s .expiration-container\").slideUp();} }'>",
-    $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id, $expiration_container_id
-  );
-
-  $form["expiration_header"] = array(
-    "#type" => "hidden",
-    "#prefix" => $expiration_prefix,
-  );
-
-  $form["has_expiration"] = array(
-    "#type" => "checkbox",
-    "#title" => t("Discount expires"),
-    "#description" => t("Whether or not this discount will be applied if other discounts are."),
-    "#default_value" => $has_expiration,
-  );
-
-  $expiration = isset($form_state["values"]["expiration"]) ? $form_state["values"]["expiration"] : time();
-  $form["expiration"] = array(
-    "#type" => "date_popup",
-    "#date_type" => "DATE_UNIX",
-    "#title" => t("Discount expiration") . sprintf("<span title='". t("This field is required.") ."' class='form-required'>*</span>"),
-    "#description" => t("Date and time when the discount expires."),
-    "#default_value" => date("Y-m-d H:i:s", $expiration),
-    "#prefix" => sprintf("<div class='expiration-container'%s>", $display_string),
-    "#suffix" => "</div>",
-  );
-
-  $form["expiration_footer"] = array(
-    "#type" => "hidden",
-    "#suffix" => "</div>",
-  );
-
   $is_published = isset($form_state["values"]["is_published"]) ? $form_state["values"]["is_published"] : IS_PUBLISHED_DEFAULT;
   $form["is_published"] = array(
     "#type" => "checkbox",
@@ -486,16 +551,6 @@ function uc_discounts_form($form_state, 
     "#default_value" => $is_published,
   );
 
-  //Add custom weight range [-50, 50]
-  $options = array();
-  for ($i = -50; $i <= 50; $i++) $options[$i] = $i;
-  $form["weight"] = array(
-    "#type" => "select",
-    "#title" => t("Weight"),
-    "#description" => t("Lighter discounts are applied to an order first. This value is unimportant if there are no discounts on discount line items."),
-    "#options" => $options,
-    "#default_value" => isset($form_state["values"]["weight"]) ? $form_state["values"]["weight"] : 0,
-  );
 
   $form["submit"] = array("#type" => "submit", "#value" => t("Submit"));
 
@@ -514,6 +569,11 @@ function uc_discounts_form_validate($for
     $has_errors = TRUE;
   }
 
+  //Check for required_product if requires_single checked
+  if ($values["requires_single_product_to_qualify"] && empty($values["required_product"])) {
+    form_set_error("required_product", t("Please select a single SKU for 'Require single product to qualify'."));
+    $has_errors = TRUE;
+  }
 
   //Check qualifying_amount_max (if necessary)
   if ($values["has_qualifying_amount_max"]) {
@@ -562,6 +622,12 @@ function uc_discounts_form_validate($for
       $has_errors = TRUE;
     }
   }
+  elseif ($values["filter_type"] == FILTER_TYPE_CLASS) {
+    if (empty($values["classes"])) {
+      form_set_error("classes", t("Product classes are required because of 'Filter Type' value"));
+      $has_errors = TRUE;
+    }
+  }
   else {
     form_set_error("filter_type", t("Invalid filter type value"));
     $has_errors = TRUE;
@@ -587,6 +653,7 @@ function uc_discounts_form_validate($for
  * Submit handler for uc_discounts_form().
  */
 function uc_discounts_form_submit($form, &$form_state) {
+  $form_state["redirect"] = "admin/store/settings/uc_discounts";
   $op = isset($form_state["values"]["op"]) ? $form_state["values"]["op"] : "";
   if ($op == t("Submit")) {
     $codes = explode("\n", $form_state["values"]["codes"]);
@@ -612,6 +679,13 @@ function uc_discounts_form_submit($form,
       $skus = array(ALL_SKUS);
     }
 
+    $classes = ($form_state["values"]["filter_type"] == FILTER_TYPE_CLASS) ? $form_state["values"]["classes"] : array();
+
+    //If classes contain "all classes", insert only all Classes row (class=<ALL_CLASSES>)
+    if (in_array(ALL_CLASSES, $classes)) {
+      $classes = array(ALL_CLASSES);
+    }
+
     $role_ids = ($form_state["values"]["has_role_filter"]) ? $form_state["values"]["role_ids"] : array();
 
     //If roles container "all roles", insert only all roles row (role_id=<ALL_ROLES>)
@@ -626,7 +700,7 @@ function uc_discounts_form_submit($form,
     else $expiration = 0;
 
     //Set discount_amount
-    if (($index = strpos($form_state["values"]["discount_amount"], "%")) !== FALSE) {
+    if (($index = strpos($form_state["values"]["discount_amount"], "%")) !== FALSE)
       $discount_amount = floatval(substr($form_state["values"]["discount_amount"], 0, $index)) / 100;
     }
     else $discount_amount = floatval($form_state["values"]["discount_amount"]);
@@ -646,6 +720,7 @@ function uc_discounts_form_submit($form,
         $form_state["values"]["filter_type"],
         $form_state["values"]["has_role_filter"],
         $form_state["values"]["requires_single_product_to_qualify"],
+        $form_state["values"]["required_product"],
         $form_state["values"]["max_times_applied"],
         $form_state["values"]["can_be_combined_with_other_discounts"],
         $form_state["values"]["max_uses"],
@@ -672,11 +747,12 @@ function uc_discounts_form_submit($form,
         $form_state["values"]["has_qualifying_amount_max"],
         $form_state["values"]["qualifying_amount_max"],
         $form_state["values"]["discount_type"],
-        $discount_amount,
+        $form_state["values"]["discount_amount"],
         $form_state["values"]["requires_code"],
         $form_state["values"]["filter_type"],
         $form_state["values"]["has_role_filter"],
         $form_state["values"]["requires_single_product_to_qualify"],
+        $form_state["values"]["required_product"],
         $form_state["values"]["max_times_applied"],
         $form_state["values"]["can_be_combined_with_other_discounts"],
         $form_state["values"]["max_uses"],
@@ -700,6 +776,9 @@ function uc_discounts_form_submit($form,
       //Delete existing database SKUs
       uc_discounts_skus_delete($discount_id);
 
+      //Delete existing database classes
+      uc_discounts_classes_delete($discount_id);
+
       //Delete existing database roles
       uc_discounts_roles_delete($discount_id);
     }
@@ -740,6 +819,14 @@ function uc_discounts_form_submit($form,
         uc_discounts_skus_insert($discount_id, $sku);
       }
     }
+    //Insert Classes (if necessary)
+    elseif ($form_state["values"]["filter_type"] == FILTER_TYPE_CLASS) {
+      foreach ($classes as $class) {
+        
+        //Empty Class is ALL_CLASSES
+        uc_discounts_classes_insert($discount_id, $class);
+      }
+    }
 
     //Insert roles
     if ($form_state["values"]["has_role_filter"]) {
@@ -754,9 +841,6 @@ function uc_discounts_form_submit($form,
     drupal_set_message(t("%name settings have been saved.", array("%name" => $form_state["values"]["name"])));
   }
 
-  $form_state["redirect"] = "admin/store/settings/uc_discounts";
-}
-
 /**
  * Copy a discount.
  */
@@ -778,6 +862,7 @@ function uc_discounts_copy($original_dis
     $discount->filter_type,
     $discount->has_role_filter,
     $discount->requires_single_product_to_qualify,
+    $discount->required_product,
     $discount->max_times_applied,
     $discount->can_be_combined_with_other_discounts,
     $discount->max_uses,
@@ -805,6 +890,10 @@ function uc_discounts_copy($original_dis
   $skus = get_skus_for_discount($original_discount_id);
   foreach ($skus as $sku) uc_discounts_skus_insert($discount_id, $sku);
 
+  //Copy discount's Classes
+  $classes = get_classes_for_discount($original_discount_id);
+  foreach ($classes as $class) uc_discounts_classes_insert($discount_id, $class);
+
   //Copy discount's roles
   $role_ids = get_role_ids_for_discount($original_discount_id);
   foreach ($role_ids as $role_id) uc_discounts_roles_insert($discount_id, $role_id);
Index: uc_discounts.css
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_discounts_alt/uc_discounts/uc_discounts.css,v
retrieving revision 1.1
diff -u -p -r1.1 uc_discounts.css
--- uc_discounts.css	20 Apr 2009 03:14:33 -0000	1.1
+++ uc_discounts.css	24 Sep 2009 22:52:01 -0000
@@ -14,3 +14,4 @@
 .uc-discounts-cart-pane-table-cell {
 	width: 100%;
 }
+
Index: uc_discounts.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_discounts_alt/uc_discounts/uc_discounts.info,v
retrieving revision 1.2
diff -u -p -r1.2 uc_discounts.info
--- uc_discounts.info	21 Feb 2009 19:28:06 -0000	1.2
+++ uc_discounts.info	24 Sep 2009 22:52:01 -0000
@@ -6,3 +6,4 @@ dependencies[] = uc_store
 dependencies[] = uc_product
 package = "Ubercart - core (optional)"
 core = 6.x
+
Index: uc_discounts.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_discounts_alt/uc_discounts/uc_discounts.install,v
retrieving revision 1.9
diff -u -p -r1.9 uc_discounts.install
--- uc_discounts.install	22 Aug 2009 21:30:39 -0000	1.9
+++ uc_discounts.install	24 Sep 2009 22:52:01 -0000
@@ -1,5 +1,5 @@
 <?php
-//$Id: uc_discounts.install,v 1.9 2009/08/22 21:30:39 ryangroe Exp $
+//$Id: uc_discounts.install,v 1.8 2009/05/27 03:57:34 ryangroe Exp $
 
 
 /**
@@ -95,6 +95,12 @@ function uc_discounts_schema() {
         "default" => 1,
         "description" => t("Requires qualifying amount to come from a single product."),
       ),
+      "required_product" => array(
+        "type" => "varchar",
+        "length" => 255,
+        "not null" => TRUE,
+        "default" => "",
+      ),
       "max_times_applied" => array(
         "type" => "int",
         "not null" => TRUE,
@@ -272,6 +278,33 @@ function uc_discounts_skus_schema() {
   return $schema;
 }
 
+function uc_discounts_classes_schema() {
+  $schema = array();
+
+  $schema["uc_discounts_classes"] = array(
+    "fields" => array(
+      "discount_class_id" => array(
+        "type" => "serial",
+        "no null" => TRUE,
+      ),
+      "discount_id" => array(
+        "type" => "int",
+        "not null" => TRUE,
+        "description" => t("The {uc_discounts}.discount_id of the discount."),
+      ),
+      "class" => array(
+        "type" => "varchar",
+        "length" => 32,
+        "not null" => TRUE,
+        "description" => t("The {node_type}.type of the product being discounted."),
+      ),
+    ),
+    "primary key" => array("discount_class_id"),
+  );
+
+  return $schema;
+}
+
 function uc_discounts_roles_schema() {
   $schema = array();
 
@@ -390,15 +423,22 @@ function uc_discounts_install() {
   drupal_install_schema("uc_discounts_roles");
   drupal_install_schema("uc_discounts_uses");
   drupal_install_schema("uc_discounts_order_codes");
+  drupal_install_schema("uc_discounts_classes");
 
   //Weight must be less than uc_payment's
   db_query("UPDATE {system} SET weight=-10 WHERE name='uc_discounts'");
+
+  //Print out a nice message directing administrators towards the configuration screen.
+  drupal_set_message(st("UC Discounts (Alternative) settings are available under !link",
+      array("!link" => l("Administer > Store administration > Configuration", "admin/store/settings/uc_discounts"))
+    ));
 }
 
 /**
  * Implementation of hook_uninstall().
  */
 function uc_discounts_uninstall() {
+  drupal_uninstall_schema("uc_discounts_classes");
   drupal_uninstall_schema("uc_discounts_order_codes");
   drupal_uninstall_schema("uc_discounts_uses");
   drupal_uninstall_schema("uc_discounts_products");
@@ -487,3 +527,30 @@ function uc_discounts_update_4() {
   return $queries;
 }
 
+//Add required_product column to uc_discounts table
+function uc_discounts_update_5() {
+  $queries = array();
+
+  switch ($GLOBALS["db_type"]) {
+    case "mysql":
+    case "mysqli":
+      $queries[] = update_sql("ALTER TABLE {uc_discounts} ADD required_product varchar(255) NOT NULL default '' AFTER requires_single_product_to_qualify");
+      break;
+
+    case "pgsql":
+      db_add_column($queries, "uc_discounts", "required_product", "varchar(255)",
+        array("not null" => TRUE, "default" => "")
+      );
+      break;
+  }
+
+  return $queries;
+}
+
+//Add uc_discounts_classes table
+function uc_discounts_update_6() {
+  $schema = uc_discounts_classes_schema();
+  db_create_table($queries, "uc_discounts_classes", $schema["uc_discounts_classes"]);
+  return $queries;
+}
+
Index: uc_discounts.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_discounts_alt/uc_discounts/uc_discounts.js,v
retrieving revision 1.5
diff -u -p -r1.5 uc_discounts.js
--- uc_discounts.js	10 Mar 2009 15:08:48 -0000	1.5
+++ uc_discounts.js	24 Sep 2009 22:52:01 -0000
@@ -1,5 +1,6 @@
 //$Id: uc_discounts.js,v 1.5 2009/03/10 15:08:48 ryangroe Exp $
 
+
 var uc_discountsLineItems = [];
 var uc_discountsisUpdating = false;
 
@@ -193,3 +194,4 @@ function uc_discountsUpdateTotal()
     if (window.getTax)
         getTax();
 }
+
Index: uc_discounts.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_discounts_alt/uc_discounts/uc_discounts.module,v
retrieving revision 1.29
diff -u -p -r1.29 uc_discounts.module
--- uc_discounts.module	22 Aug 2009 21:39:12 -0000	1.29
+++ uc_discounts.module	24 Sep 2009 22:52:01 -0000
@@ -1,5 +1,5 @@
 <?php
-//$Id: uc_discounts.module,v 1.29 2009/08/22 21:39:12 ryangroe Exp $
+//$Id: uc_discounts.module,v 1.26 2009/05/26 01:45:13 ryangroe Exp $
 
 
 /**
@@ -21,10 +21,12 @@ define("DISCOUNT_TYPE_PERCENTAGE_OFF_PER
 define("FILTER_TYPE_PRODUCTS", 1);
 define("FILTER_TYPE_TERMS", 2);
 define("FILTER_TYPE_SKUS", 3);
+define("FILTER_TYPE_CLASS", 4);
 
 define("ALL_PRODUCTS", "-1");
 define("ALL_TERMS", "-1");
 define("ALL_SKUS", "");
+define("ALL_CLASSES", "");
 define("ALL_ROLES", "-1");
 
 define("REQUIRES_SINGLE_PRODUCT_TO_QUALIFY_DEFAULT", FALSE);
@@ -268,6 +270,10 @@ function uc_discounts_order($op, &$arg1,
 
 /**
  * Implementation of Ubercart's hook_line_item().
+ *
+ * Displays all discounts as a single line item
+ *
+ * @see hook_line_item()
  */
 function uc_discounts_line_item() {
   $line_items[] = array(
@@ -284,6 +290,8 @@ function uc_discounts_line_item() {
 
 /**
  * Implementation of hook_cart_pane().
+ *
+ * @see hook_cart_pane()
  */
 function uc_discounts_cart_pane($items) {
   global $user;
@@ -339,6 +347,8 @@ function uc_discounts_cart_pane($items) 
 
 /**
  * Implementation of hook_checkout_pane().
+ *
+ * @see hook_checkout_pane()
  */
 function uc_discounts_checkout_pane() {
   $panes[] = array(
@@ -554,7 +564,7 @@ function get_uc_discounts_column_names()
   return array("name", "short_description", "description", "qualifying_type",
     "qualifying_amount", "has_qualifying_amount_max", "qualifying_amount_max", "discount_type",
     "discount_amount", "requires_code", "filter_type", "has_role_filter",
-    "requires_single_product_to_qualify", "max_times_applied", "can_be_combined_with_other_discounts", "max_uses",
+    "requires_single_product_to_qualify", "required_product", "max_times_applied", "can_be_combined_with_other_discounts", "max_uses",
     "max_uses_per_user", "max_uses_per_code", "has_expiration", "expiration",
     "is_published", "weight",
   );
@@ -581,7 +591,7 @@ function get_uc_discounts_column_printf_
   return array("'%s'", "'%s'", "'%s'", "%d",
     "%f", "%d", "%f", "%d",
     "%f", "%d", "%d", "%d",
-    "%d", "%d", "%d", "%d",
+    "%d", "'%s'", "%d", "%d", "%d",
     "%d", "%d", "%d", "%d",
     "%d", "%d",
   );
@@ -607,7 +617,7 @@ function get_uc_discounts_column_update_
 function uc_discounts_insert($name, $short_description, $description, $qualifying_type,
   $qualifying_amount, $has_qualifying_amount_max, $qualifying_amount_max, $discount_type,
   $discount_amount, $requires_code, $filter_type, $has_role_filter,
-  $requires_single_product_to_qualify, $max_times_applied, $can_be_combined_with_other_discounts, $max_uses,
+  $requires_single_product_to_qualify, $required_product, $max_times_applied, $can_be_combined_with_other_discounts, $max_uses,
   $max_uses_per_user, $max_uses_per_code, $has_expiration, $expiration,
   $is_published, $weight, $insert_timestamp = NULL
 ) {
@@ -621,7 +631,7 @@ function uc_discounts_insert($name, $sho
     $name, $short_description, $description, $qualifying_type,
     $qualifying_amount, $has_qualifying_amount_max, $qualifying_amount_max, $discount_type,
     $discount_amount, $requires_code, $filter_type, $has_role_filter,
-    $requires_single_product_to_qualify, $max_times_applied, $can_be_combined_with_other_discounts, $max_uses,
+    $requires_single_product_to_qualify, $required_product, $max_times_applied, $can_be_combined_with_other_discounts, $max_uses,
     $max_uses_per_user, $max_uses_per_code, $has_expiration, $expiration,
     $is_published, $weight, $insert_timestamp
   );
@@ -637,7 +647,7 @@ function uc_discounts_update($discount_i
   $name, $short_description, $description, $qualifying_type,
   $qualifying_amount, $has_qualifying_amount_max, $qualifying_amount_max, $discount_type,
   $discount_amount, $requires_code, $filter_type, $has_role_filter,
-  $requires_single_product_to_qualify, $max_times_applied, $can_be_combined_with_other_discounts, $max_uses,
+  $requires_single_product_to_qualify, $required_product, $max_times_applied, $can_be_combined_with_other_discounts, $max_uses,
   $max_uses_per_user, $max_uses_per_code, $has_expiration, $expiration,
   $is_published, $weight
 ) {
@@ -660,7 +670,7 @@ function uc_discounts_update($discount_i
     $name, $short_description, $description, $qualifying_type,
     $qualifying_amount, $has_qualifying_amount_max, $qualifying_amount_max, $discount_type,
     $discount_amount, $requires_code, $filter_type, $has_role_filter,
-    $requires_single_product_to_qualify, $max_times_applied, $can_be_combined_with_other_discounts, $max_uses,
+    $requires_single_product_to_qualify, $required_product, $max_times_applied, $can_be_combined_with_other_discounts, $max_uses,
     $max_uses_per_user, $max_uses_per_code, $has_expiration, $expiration,
     $is_published, $weight, $discount_id
   );
@@ -713,6 +723,11 @@ function uc_discounts_codes_delete($disc
 /**
  * Returns product_ids for discount object.
  * Note: this function correctly returns all products for term-based discounts.
+ *
+ * @param $discount object
+ * @param $exclude_all_products bool
+ *
+ * @return array of product ids (nids)
  */
 function get_product_ids_for_discount_object($discount, $exclude_all_products = FALSE) {
   switch ($discount->filter_type) {
@@ -740,6 +755,15 @@ function get_product_ids_for_discount_ob
       $result = db_query($query, $discount->discount_id);
       while ($row = db_fetch_array($result)) $product_ids[] = $row["nid"];
       return $product_ids;
+
+    case FILTER_TYPE_CLASS:
+      $query = "SELECT DISTINCT n.nid FROM {node} n
+				INNER JOIN {uc_discounts_classes} dcl ON n.type=dcl.class
+				WHERE dcl.discount_id=%d";
+      uc_discounts_log($query);
+      $result = db_query($query, $discount->discount_id);
+      while ($row = db_fetch_array($result)) $product_ids[] = $row["nid"];
+      return $product_ids;
   }
   return array();
 }
@@ -748,6 +772,11 @@ function get_product_ids_for_discount_ob
  * Returns product_ids for discount.
  * Note: this function does not check filter_type so a discount with filter_type other than
  *	  FILTER_TYPE_PRODUCTS will return no values.
+ *
+ * @param $discount_id
+ * @param $exclude_all_products
+ *
+ * @return array of product ids (nids)
  */
 function get_product_ids_for_discount($discount_id, $exclude_all_products = FALSE) {
   $product_ids = array();
@@ -820,6 +849,24 @@ function get_skus_for_discount($discount
 }
 
 /**
+ * Returns Product Class names for discount.
+ */
+function get_classes_for_discount($discount_id, $exclude_all_classes = FALSE) {
+  $classes = array();
+
+  //Get Classes from database
+  if ($exclude_all_classes) {
+    $result = db_query("SELECT class FROM {uc_discounts_classes} WHERE discount_id=%d AND class<>%d",
+      $discount_id, ALL_CLASSES
+    );
+  }
+  else $result = db_query("SELECT class FROM {uc_discounts_classes} WHERE discount_id=%d", $discount_id);
+
+  while ($row = db_fetch_array($result)) $classes[] = $row["class"];
+  return $classes;
+}
+
+/**
  * Returns role_ids for discount.
  */
 function get_role_ids_for_discount($discount_id, $exclude_all_roles = FALSE) {
@@ -876,6 +923,25 @@ function uc_discounts_skus_delete($disco
 }
 
 /**
+ * Inserts a uc_discounts_classes row and returns its discount_class_id.
+ */
+function uc_discounts_classes_insert($discount_id, $class) {
+  $query = "INSERT INTO {uc_discounts_classes}(discount_id, class) VALUES(%d, '%s')";
+  uc_discounts_log("query=". $query);
+  db_query($query, $discount_id, $class);
+  return db_last_insert_id("uc_discounts_classes", "discount_class_id");
+}
+
+/**
+ * Deletes all uc_discounts_classes rows for a discount.
+ */
+function uc_discounts_classes_delete($discount_id) {
+  $query = "DELETE FROM {uc_discounts_classes} WHERE discount_id=%d";
+  uc_discounts_log("query=". $query);
+  db_query($query, $discount_id);
+}
+
+/**
  * Inserts a uc_discounts_roles row and returns its discount_role_id.
  */
 function uc_discounts_roles_insert($discount_id, $role_id) {
@@ -901,12 +967,19 @@ function uc_discounts_roles_delete($disc
  * @param $order Order to get discounts for
  * @param $errors Reference to array to add error messages to
  * @param $messages Reference to array to add success messages to
+ *
+ * @return array of discount objects
  */
 function get_discounts_for_order($order, &$errors = NULL, &$warnings = NULL, &$messages = NULL) {
+  // Product NIDS in cart => subtotal of individual item
   $order_product_id_subtotal_map = array();
+  // Product NIDS in cart => quantity of individual item
   $order_product_id_quantity_map = array();
+  // Product NIDS in cart
   $order_product_ids = array();
+  // Product NIDS in cart=> bool
   $order_product_ids_set = array();
+  // Product objects in cart
   $order_product_id_product_array_map = array();
   $order_subtotal = 0;
 
@@ -933,6 +1006,7 @@ function get_discounts_for_order($order,
     }
   }
 
+  // Populate product NID array with NIDs from the order
   $order_product_ids = array_keys($order_product_ids_set);
 
   $temp_product_ids   = $order_product_ids;
@@ -962,12 +1036,24 @@ function get_discounts_for_order($order,
   if (is_array($order->products) && !empty($order->products)) {
     //Get SKUs for order's products
     $result = db_query("SELECT DISTINCT model FROM {uc_products} WHERE nid IN(%s)", join(",", $order_product_ids));
-    while ($row = db_fetch_array($result)) $temp_skus[] = "'". db_escape_string($row["model"]) ."'";;
+    while ($row = db_fetch_array($result)) $temp_skus[] = "'". db_escape_string($row["model"]) ."'";
   }
   $skus_clause = sprintf("d.filter_type<>%d OR ds.sku IN(%s)",
     FILTER_TYPE_SKUS, join(",", $temp_skus)
   );
 
+  //Create IN string of classes in order
+  $temp_classes = array();
+  $temp_classes[] = "'". db_escape_string(ALL_CLASSES) ."'";
+  if (is_array($order->products) && !empty($order->products)) {
+    //Get classes for order's products
+    $result = db_query("SELECT DISTINCT type FROM {node} WHERE nid IN(%s)", join(",", $order_product_ids));
+    while ($row = db_fetch_array($result)) $temp_classes[] = "'". db_escape_string($row["type"]) ."'";
+  }
+  $classes_clause = sprintf("d.filter_type<>%d OR dcl.class IN(%s)",
+    FILTER_TYPE_CLASS, join(",", $temp_classes)
+  );
+
   //Create codes clause
   $escaped_codes_string = NULL;
   if (!empty($order->uc_discounts_codes)) {
@@ -996,6 +1082,7 @@ function get_discounts_for_order($order,
 			LEFT JOIN {uc_discounts_skus} ds ON d.discount_id=ds.discount_id
 			LEFT JOIN {uc_discounts_roles} dr ON d.discount_id=dr.discount_id
 			LEFT JOIN {uc_discounts_codes} dc ON d.discount_id=dc.discount_id
+			LEFT JOIN {uc_discounts_classes} dcl ON d.discount_id=dcl.discount_id
 			WHERE dc.code IN(%s)
 			AND (d.has_role_filter=0%s)
 			AND (%s)
@@ -1003,7 +1090,7 @@ function get_discounts_for_order($order,
 			AND (%s)
 			AND (d.has_expiration<>0 AND d.expiration<=%d)
 			ORDER BY weight", $escaped_codes_string, $roles_clause, $product_ids_clause,
-      $term_ids_clause, $skus_clause, time()
+      $term_ids_clause, $skus_clause, $classes_clause, time()
     );
     uc_discounts_log("query=". $query);
     $result = db_query($query);
@@ -1018,20 +1105,21 @@ function get_discounts_for_order($order,
 		LEFT JOIN {uc_discounts_products} dp ON d.discount_id=dp.discount_id
 		LEFT JOIN {uc_discounts_terms} dt ON d.discount_id=dt.discount_id
 		LEFT JOIN {uc_discounts_skus} ds ON d.discount_id=ds.discount_id
+		LEFT JOIN {uc_discounts_classes} dcl ON d.discount_id=dcl.discount_id
 		WHERE (d.requires_code=0%s)
 		AND (d.has_role_filter=0%s)
 		AND (%s)
 		AND (%s)
-		AND (%s)
 		AND (d.has_expiration=0 OR d.expiration>%d)
 		ORDER BY weight", $codes_clause, $roles_clause, $product_ids_clause,
-    $term_ids_clause, $skus_clause, time()
+    $term_ids_clause, $skus_clause, $classes_clause, time()
   );
   uc_discounts_log("query=". $query);
   $result = db_query($query);
 
   $total_discount_amount = 0;
   $discounts = array();
+  // Appears to check if order qualifies for each discount then applies discount.  Functionality should be separated, no?
   while ($discount = db_fetch_object($result)) {
     //Get code for discount (if one exists)
     $discount->code = NULL;
@@ -1048,7 +1136,7 @@ function get_discounts_for_order($order,
     //The query handled valid codes and expiration, this block must:
     //	check max uses (if applicable)
     //	check if discount is being combined and can be combined
-    //	check if order qualifies (type, requires_single_product_to_qualify, can_be_combined_with_other_discounts)
+    //	check if order qualifies (type, requires_single_product_to_qualify, required_product, can_be_combined_with_other_discounts)
     //	determine number of times to apply discount
 
     //If this discount has a max uses amount, check max uses
@@ -1087,7 +1175,7 @@ function get_discounts_for_order($order,
       $discount->user_uses_count = $row["user_uses_count"];
     }
 
-    //If code exists and this discount has a max uses per code amount, check max uses per user
+    //If code exists and this discount has a max uses per code amount, check max uses per code
     if (!is_null($discount->code) && ($discount->max_uses_per_code > 0)) {
       $row = db_fetch_array(db_query("SELECT COUNT(*) as code_uses_count FROM {uc_discounts_uses} WHERE discount_id=%d AND code='%s'",
           $discount->discount_id, $discount->code
@@ -1117,6 +1205,7 @@ function get_discounts_for_order($order,
         continue;
       }
 
+      // Check if the first discount can't be combined
       if (!$discounts[0]->can_be_combined_with_other_discounts) {
         //If first discount was a coded discount, add error message (only print warning if both
         //discounts have codes)
@@ -1129,8 +1218,7 @@ function get_discounts_for_order($order,
       }
     }
 
-
-    //Check if order qualifies for this discount (check type and requires_single_product_to_qualify)
+    //Check if order qualifies for this discount (check type, requires_single_product_to_qualify, required_product)
 
     //Get product IDs for discount
     $discount_product_ids = get_product_ids_for_discount_object($discount);
@@ -1138,7 +1226,6 @@ function get_discounts_for_order($order,
       $discount_product_ids = $order_product_ids;
     }
 
-
     //Determine total qualifying amount of order (store in order_qualifying_amount)
     //and qualifying subtotal of order (store in order_qualifying_subtotal)
 
@@ -1147,29 +1234,36 @@ function get_discounts_for_order($order,
 
     //If discount requires a single product
     if ($discount->requires_single_product_to_qualify) {
-      switch ($discount->qualifying_type) {
-        case QUALIFYING_TYPE_MINIMUM_PRICE:
-          //Determine the maximum product subtotal
-          foreach ($discount_product_ids as $product_id) {
-            if (isset($order_product_id_subtotal_map[$product_id])) {
-              $order_qualifying_amount = max($order_qualifying_amount, $order_product_id_subtotal_map[$product_id]);
-            }
-          }
+      // Check if required_product is set or trust that form validation did the right thing?  Check anyway...
+      if ($discount->required_product) {
 
-          //Subtract already discounted amount
-          $order_qualifying_amount -= $total_discount_amount;
-          //TO DO: keep track of each item's subtotal and decrement upon applying discount
-          break;
+        // Get nid of required_product
+        $required_product_result = db_query("SELECT DISTINCT nid from {uc_products} WHERE model=('%s')", $discount->required_product);
+        $required_product_id = db_result($required_product_result);
+
+        // Check if required_product nid is in cart
+        if (in_array($required_product_id, $order_product_ids)) {
+          switch ($discount->qualifying_type) {
+            // Buy $50 of X product
+
+            case QUALIFYING_TYPE_MINIMUM_PRICE:
+              //Determine the maximum product subtotal
+              $order_qualifying_amount = max($order_qualifying_amount, $order_product_id_subtotal_map[$required_product_id]);
+
+              //Subtract already discounted amount
+              $order_qualifying_amount -= $total_discount_amount;
+              //TO DO: keep track of each item's subtotal and decrement upon applying discount
+              break;
 
-        case QUALIFYING_TYPE_MINIMUM_QUANTITY:
-          //Determine the maximum product quantity
-          foreach ($discount_product_ids as $product_id) {
-            if (isset($order_product_id_quantity_map[$product_id])) {
-              $order_qualifying_amount = max($order_qualifying_amount, $order_product_id_quantity_map[$product_id]);
-            }
+            // Buy 5 of X product...
+
+            case QUALIFYING_TYPE_MINIMUM_QUANTITY:
+              //Determine the maximum product quantity
+              $order_qualifying_amount = max($order_qualifying_amount, $order_product_id_quantity_map[$required_product_id]);
+              //TO DO: keep track of each item's quantity and decrement upon applying discount
+              break;
           }
-          //TO DO: keep track of each item's quantity and decrement upon applying discount
-          break;
+        }
       }
     }
     //Otherwise sum product subtotals or quantities
@@ -1255,7 +1349,7 @@ function get_discounts_for_order($order,
     //Get product IDs in order that are in discount
     $order_and_discount_product_ids = array_intersect($discount_product_ids, $order_product_ids);
 
-    //Create array of product objects related to this discount
+    //Create array of product objects in cart to which this discount gets applied.
     $order_and_discount_products = array();
     foreach ($order_and_discount_product_ids as $product_id) {
       if (array_key_exists($product_id, $order_product_id_product_array_map)) {
@@ -1269,12 +1363,13 @@ function get_discounts_for_order($order,
       case DISCOUNT_TYPE_PERCENTAGE_OFF_PER_QUALIFYING_ITEM:
         $order_and_discount_product_ids = array_intersect($discount_product_ids, $order_product_ids);
         foreach ($order->products as $this_product) {
+        	drupal_set_message("testing product $this_product->nid for qualificaiton");
           if (in_array($this_product->nid, $order_and_discount_product_ids)) {
             $this_product_price = $this_product->price;
             $product_sum = $product_sum + $this_product_price;
           }
-          $discount->amount = $product_sum - ($product_sum * $discount->discount_amount);
         }
+        $discount->amount = $product_sum - ($product_sum * $discount->discount_amount);
         break;
       case DISCOUNT_TYPE_FREE_ITEMS:
         //The variable discount_amount is the monitary amount of discount
@@ -1443,6 +1538,7 @@ function get_discounts_for_order($order,
     //Add this discount to list of discounts applied to order
     $discounts[] = $discount;
   }
+  // end of db fetch while loop
   return $discounts;
 }
 
@@ -1496,6 +1592,12 @@ function get_codeless_discounts_for_prod
   $result = db_query("SELECT DISTINCT model FROM {uc_products} WHERE nid=%d", $product->nid);
   while ($row = db_fetch_array($result)) $skus[] = "'". db_escape_string($row["model"]) ."'";
 
+  //Get classes for product
+  $classes   = array();
+  $classes[] = "'". db_escape_string(ALL_CLASSES) ."'";
+  $result    = db_query("SELECT DISTINCT type FROM {node} WHERE nid=%d", $product->nid);
+  while ($row = db_fetch_array($result)) $classes[] = "'". db_escape_string($row["type"]) ."'";
+
   //Create roles clause
   global $user;
   $auth_rid = ($user->uid != 0) ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
@@ -1516,19 +1618,24 @@ function get_codeless_discounts_for_prod
     FILTER_TYPE_SKUS, join(",", $skus)
   );
 
+  $classes_clause = sprintf("d.filter_type<>%d OR dcl.class IN(%s)",
+    FILTER_TYPE_CLASS, join(",", $classes)
+  );
+
   $sort_order_string = (is_ascending_sort) ? "ASC" : "DESC";
 
   $query = sprintf("SELECT d.* FROM {uc_discounts} d
 		LEFT JOIN {uc_discounts_products} dp ON d.discount_id=dp.discount_id
 		LEFT JOIN {uc_discounts_terms} dt ON d.discount_id=dt.discount_id
 		LEFT JOIN {uc_discounts_skus} ds ON d.discount_id=ds.discount_id
+		LEFT JOIN {uc_discounts_classes} dcl ON d.discount_id=dcl.discount_id
 		WHERE d.requires_code=0
 		AND (d.has_role_filter=0%s)
 		AND (%s)
 		AND (%s)
 		AND (%s)
 		AND (d.has_expiration=0 OR d.expiration>%d)
-		ORDER BY d.%s %s", $roles_clause, $product_ids_clause, $term_ids_clause, $skus_clause, time(),
+		ORDER BY d.%s %s", $roles_clause, $product_ids_clause, $term_ids_clause, $skus_clause, $classes_clause, time(),
     $sort_column, $sort_order_string
   );
   uc_discounts_log("query=". $query);
