Index: uc_order/uc_order.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ubercart/uc_order/uc_order.install,v
retrieving revision 1.9.2.14
diff -u -p -r1.9.2.14 uc_order.install
--- uc_order/uc_order.install	21 Jul 2009 14:37:18 -0000	1.9.2.14
+++ uc_order/uc_order.install	17 Aug 2009 04:08:34 -0000
@@ -43,6 +43,13 @@ function uc_order_schema() {
         'not null' => TRUE,
         'default' => 0.0,
       ),
+      'product_count' => array(
+        'description' => t('The total product quantity of the order.'),
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'default' => 0,
+      ),
       'primary_email' => array(
         'description' => t('The email address of the customer.'),
         'type' => 'varchar',
@@ -1006,3 +1013,24 @@ function uc_order_update_6010(&$sandbox)
 
   return $ret;
 }
+
+function uc_order_update_6011() {
+  $ret = array();
+
+  $spec = array(
+    'description' => t('The total product quantity of the order.'),
+    'type' => 'int',
+    'unsigned' => TRUE,
+    'not null' => TRUE,
+    'default' => 0,
+  );
+  db_add_field($ret, 'uc_orders', 'product_count', $spec);
+
+  $result = db_query("SELECT order_id FROM {uc_orders} ORDER BY order_id ASC");
+  while ($row = db_fetch_object($result)) {
+    $row->product_count = db_result(db_query("SELECT SUM(qty) FROM {uc_order_products} WHERE order_id = %d", $row->order_id));
+    drupal_write_record('uc_orders', $row, 'order_id');
+  }
+
+  return $ret;
+}
Index: uc_order/uc_order.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ubercart/uc_order/uc_order.module,v
retrieving revision 1.12.2.28
diff -u -p -r1.12.2.28 uc_order.module
--- uc_order/uc_order.module	29 Jul 2009 19:54:18 -0000	1.12.2.28
+++ uc_order/uc_order.module	17 Aug 2009 04:08:36 -0000
@@ -1038,7 +1038,7 @@ function uc_order_save($order) {
     return FALSE;
   }
 
-  db_query("UPDATE {uc_orders} SET uid = %d, order_status = '%s', order_total = %f, primary_email = '%s', "
+  db_query("UPDATE {uc_orders} SET uid = %d, order_status = '%s', order_total = %f, product_count = %d, primary_email = '%s', "
           ."delivery_first_name = '%s', delivery_last_name = '%s', delivery_phone = '%s', "
           ."delivery_company = '%s', delivery_street1 = '%s', delivery_street2 = '%s', "
           ."delivery_city = '%s', delivery_zone = %d, delivery_postal_code = '%s', delivery_country = %d, "
@@ -1046,7 +1046,7 @@ function uc_order_save($order) {
           ."billing_company = '%s', billing_street1 = '%s', billing_street2 = '%s', "
           ."billing_city = '%s', billing_zone = %d, billing_postal_code = '%s', billing_country = %d, "
           ."payment_method = '%s', data = '%s', host = '%s', modified = %d WHERE order_id = %d",
-           $order->uid, $order->order_status, uc_order_get_total($order),
+           $order->uid, $order->order_status, uc_order_get_total($order), uc_order_get_product_count($order),
            $order->primary_email, $order->delivery_first_name, $order->delivery_last_name, $order->delivery_phone,
            $order->delivery_company, $order->delivery_street1, $order->delivery_street2,
            $order->delivery_city, $order->delivery_zone, $order->delivery_postal_code,
@@ -1154,6 +1154,11 @@ function uc_order_load($order_id) {
     $order->order_total = $total;
   }
 
+  if (($count = uc_order_get_product_count($order)) !== $order->product_count) {
+    db_query("UPDATE {uc_orders} SET product_count = %d WHERE order_id = %d", $count, $order->order_id);
+    $order->product_count = $count;
+  }
+
   return $order;
 }
 
@@ -1430,6 +1435,21 @@ function uc_order_get_total($order, $pro
 }
 
 /**
+ * Calculate up an order's product count
+ */
+function uc_order_get_product_count($order) {
+  $count = 0;
+
+  if (is_array($order->products)) {
+    foreach ($order->products as $product) {
+      $count += $product->qty;
+    }
+  }
+
+  return $count;
+}
+
+/**
  * An order can be shipped if any of its products can be shipped.
  */
 function uc_order_is_shippable($order) {
