Index: /Users/torgospizza/Documents/Drupal Work/contributions/modules/uc_upsell-DRUPAL-6--1/uc_upsell_core.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_upsell/uc_upsell_core.inc,v
retrieving revision 1.1.2.28
diff -u -r1.1.2.28 uc_upsell_core.inc
--- uc_upsell_core.inc	4 Dec 2009 05:07:09 -0000	1.1.2.28
+++ uc_upsell_core.inc	6 Dec 2009 22:17:59 -0000
@@ -73,7 +73,7 @@
     // We are on the cart, so we need to use $cart_nids as the source, as well as filter.
     $cartnids = uc_upsell_get_cart_nids();
 
-    $related = uc_upsell_get_associate_nids($cartnids, intval($vars['max']), $cartnids);
+    $related = uc_upsell_get_associate_nids($cartnids, intval($vars['max']), $cartnids, $vars);
     if (!empty($related)) {
       $primary = 1;
       $sources += 1;
@@ -180,21 +180,31 @@
 
   // First, if we are including related-by-taxonomy nodes, and an array exists, only include as many as needed.
   if (count($related_by_term) > 0 && count($related) < $max) {
+  
     $needed = $max - count($related); // How many do we need?
 
     if ($needed > 0) {
       // Filter out the nodes already in our list
       $related_by_term = array_diff(array_values($related_by_term), array_values($related));
-      $related_by_term = array_slice($related_by_term, 0, $needed);
       $related = array_unique(array_merge((array)$related, (array)$related_by_term));
     }
   }
-  
+
+  // Remove found nids with filter and already sought nids, make them unique
+  $ary = uc_upsell_get_types_array($related);
+  $ary = uc_upsell_filter_types($ary);
+  $ary = array_diff(array_keys($ary), $filter, $sought);
+  $ary = array_unique($ary);
+
   // If count of related nodes doesn't meet threshold and can include random nodes, add them.
-  if (count($related) < $max && $vars['use_random']) {
+  
+  /*** Notes: This code should be moved into its own method, since it's "context agnostic." 
+     - Should find a way to honor the product types settings (it currently does not) ****/
+  
+  if (count($ary) < $max && $vars['use_random']) {
 
     $random_nids = array();
-    $filter_query = implode(",", array_merge($related, uc_upsell_get_cart_nids(), array((isset($product_nid)) ? $product_nid : 0)));
+    $filter_query = implode(",", array_merge($ary, uc_upsell_get_cart_nids()));
 
     $randoms = db_query("SELECT `nid`
                 FROM {node}
@@ -211,15 +221,9 @@
       $sources += 1;
     }
 
-    $related = array_unique(array_merge($related, array_slice($random_nids, 0, $max - count($related))));
+    $ary = array_unique(array_merge($related, array_slice($random_nids, 0, $max - count($related))));
   }
-
-  // Remove found nids with filter and already sought nids, make them unique
-  $ary = uc_upsell_get_types_array($related);
-  $ary = uc_upsell_filter_types($ary);
-  $ary = array_diff(array_keys($ary), $filter, $sought);
-  $ary = array_unique($ary);
-
+  
   // Allow other modules to alter associate array
   drupal_alter('uc_upsell_related', $ary);
 
