--- uc_out_of_stock/uc_out_of_stock.module.performance	2011-06-27 14:45:55.992714845 -0400
+++ uc_out_of_stock/uc_out_of_stock.module	2011-06-27 15:41:45.415660936 -0400
@@ -10,11 +10,19 @@
  * Implementation of hook_form_alter()
  */
 function uc_out_of_stock_form_alter(&$form, $form_state, $form_id) {
+  static $added_setting;
   $forms = array('uc_product_add_to_cart_form', 'uc_catalog_buy_it_now_form');
   foreach ($forms as $id) {
     if ( substr($form_id, 0, strlen($id)) == $id ) {
       drupal_add_js(drupal_get_path('module', 'uc_out_of_stock') . '/uc_out_of_stock.js');
       drupal_add_css(drupal_get_path('module', 'uc_out_of_stock') . '/uc_out_of_stock.css');
+      if (!isset($added_setting)) {
+        drupal_add_js(array(
+          'uc_out_of_stock' => array(
+            'msg' => check_markup(variable_get('uc_out_of_stock_text', UC_OUT_OF_STOCK_DEFAULT_HTML), variable_get('uc_out_of_stock_format', filter_fallback_format()), FALSE),
+        )), 'setting');
+        $added_setting = TRUE;
+      }
       $form['#validate'][] = 'uc_out_of_stock_validate_form_addtocart';
     }
   }
@@ -137,29 +145,50 @@
   return $stockinfo;
 }
 
+/**
+ * Called via AJAX to determine stock for one or more products
+ * Expects the following variables:
+ * $_POST['form_ids'] => An array of form ids
+ * $_POST['node_ids'] => A corresponding array of node ids
+ * $_POST['attr_ids'] => An array of nid:attrib_id:value based on the attribute selected
+ *
+ * Returns an array of array(form ids => stock value or NULL)
+ */
 function uc_out_of_stock_query() {
-  $response = array();
-  $attrs = array();
-
-  $nid = $_POST['nid'];
-  foreach ( $_POST as $key => $value ) {
-    if ( substr($key, 0, 4) == 'attr' ) {
-      $attrs[substr($key, 4)] = $value;
-    }
+  if (count($_POST['form_ids']) != count($_POST['node_ids'])) {
+    print 'Invalid data posted.';
   }
 
-  $stockinfo = uc_out_of_stock_getstockinfo($nid, $attrs);
-  if ($stockinfo) {
-    $response['stock'] = $stockinfo['stock'];
-    if ( $response['stock'] <= 0 ) {
-      $response['html'] = check_markup(variable_get('uc_out_of_stock_text', UC_OUT_OF_STOCK_DEFAULT_HTML), variable_get('uc_out_of_stock_format', FILTER_FORMAT_DEFAULT), FALSE);
+  $return = array_combine($_POST['form_ids'], array_fill(0, count($_POST['form_ids']), NULL));
+  // If no attributes given we can do one query to fetch everything
+  if (empty($_POST['attr_ids'])) {
+    $result = db_query('SELECT ups.stock, up.nid
+                        FROM {uc_products} up
+                        LEFT JOIN {uc_product_stock} ups ON ups.sku = up.model
+                        WHERE up.nid IN(:nids)
+				AND ups.active = 1', array(':nids' => $_POST['node_ids']));
+    foreach ($result->fetchAll() as $product) {
+      $key = array_search($product->nid, $_POST['node_ids']);
+      $return[$_POST['form_ids'][$key]] = $product->stock;
+    }
+  }
+  else {
+    $attribs = array();
+    foreach ($_POST['attr_ids'] as $value) {
+      list($nid, $attr_id, $attr_val) = explode(':', $value);
+      $attribs[$nid][$attr_id] = $attr_val;
+    }
+
+    foreach ($_POST['node_ids'] as $key => $nid) {
+      $stockinfo = uc_out_of_stock_getstockinfo($nid, (array) $attribs[$nid]);
+      if ($stockinfo) {
+        $return[$_POST['form_ids'][$key]] = $stockinfo['stock'];
+      }
     }
   }
 
-  // if there is some response, print it
-  if (count($response)){
-    print implode('|', $response);
-  }
+  print drupal_json_encode($return);
+  exit;
 }
 
 function uc_out_of_stock_settings() {
