--- uc_discount.module	2009-08-21 09:21:09.000000000 -0400
+++ uc_discount.module.new	2010-01-18 16:04:14.000000000 -0500
@@ -373,6 +373,12 @@ function uc_discount_discount_registry()
     'form' => 'uc_discount_line_items_discount_form',
     'context' => 'order',
   );
+  $discounts['product_terms'] = array(
+    'title' => t('Product taxonomy terms'),
+    'callback' => 'uc_discount_order_product_term_discount',
+    'form' => 'uc_discount_order_product_term_discount_form',
+    'context' => 'order',
+  );
   $discounts['nodes'] = array(
     'title' => t('Products'),
     'callback' => 'uc_discount_node_discount',
@@ -409,6 +415,54 @@ function uc_discount_order_product_disco
 
   return $multiplier;
 }
+/**
+ * Use the selected terms to determine if discount should be applied
+ * The terms will also be recursive, meaning any subterms associated
+ * with a product will also cause the discount to be applied to the product
+ */
+function uc_discount_order_product_term_discount($order, $settings) {
+  $multiplier = 0;
+  // get term and sub-terms for $settings['terms'] id
+  $all_terms = isset($settings['terms']) ? ($settings['terms']): array();
+    
+  foreach ($order->products as $product) {
+    // get terms for each product
+    $product_node = node_load($product->nid);
+    $node_terms = array();
+    $node_terms_obj = taxonomy_node_get_terms($product_node);
+    foreach($node_terms_obj as $oTerm) {
+      array_push($node_terms,$oTerm->tid);
+    }
+    
+    // for each term, include all of its parents in the search
+    $all_node_terms = array();
+    foreach ($node_terms as $tid) {
+      $parent_terms = array();
+      foreach(taxonomy_get_parents_all($tid) as $oTerm) {
+        array_push($parent_terms,$oTerm->tid);
+      }
+      $all_node_terms = array_merge($parent_terms,$all_node_terms);
+    }
+
+    // if any terms for product match any terms in the CA terms field
+    $term_match = 0;
+    foreach ($all_terms as $t) {
+      foreach ($all_node_terms as $v) {
+        if($t == $v) {
+          $term_match = 1;
+          break;
+        }
+      }
+      if($term_match) { break; }
+    }
+
+    // then apply the discount
+    if($term_match) {
+      $multiplier += $product->price * $product->qty;
+    }
+  }
+  return $multiplier; 
+}
 
 function uc_discount_order_product_discount_form($form_state, $settings = array()) {
   $form = array();
@@ -432,6 +486,21 @@ function uc_discount_order_product_disco
 }
 
 /**
+ * Create the form to select taxonomy terms to apply discount to
+ */
+function uc_discount_order_product_term_discount_form($form_state, $settings = array()) {
+  // use the Catalog vocabulary
+  $result = db_query("SELECT vid from {vocabulary} WHERE name = 'Catalog'");
+  if($vocab = db_fetch_object($result)) {
+    $vid = $vocab->vid;
+  }
+  $values = isset($settings['terms']) ? ($settings['terms']) : array();
+  // use the form builder from the taxonomy module
+  $form['terms'] = taxonomy_form($vid,$values);
+  return $form;
+}
+
+/**
  * Validate an autocomplete element.
  *
  * Remove the wrapper layer and set the right element's value.
