=== modified file 'uc_stock/uc_stock.module'
--- uc_stock/uc_stock.module	2009-05-07 20:12:13 +0000
+++ uc_stock/uc_stock.module	2009-05-11 14:45:19 +0000
@@ -102,6 +102,32 @@
   }
 }
 
+/**
+ * Implementation of hook_nodeapi().
+ */
+function uc_stock_nodeapi(&$node, $op) {
+  switch ($op) {
+    case 'delete':
+      if (uc_product_is_product($node)) {
+        $models = uc_product_get_models($node);
+        unset($models['']); // Remove "Any" model.
+
+        foreach ($models as $model) {
+          // If any other product has the same sku as the deleted node, grant it
+          // "ownership" of the stock data. Otherwise, delete the stock data.
+          $result = db_query_range("SELECT nid, model FROM {uc_products} WHERE model = '%s' ORDER BY nid", $model, 0, 1);
+          if ($same_sku = db_fetch_object($result)) {
+            db_query("UPDATE {uc_product_stock} SET nid = %d WHERE sku = '%s'", $same_sku->nid, $same_sku->model);
+          }
+          else {
+            db_query("DELETE FROM {uc_product_stock} WHERE sku = '%s'", $model);
+          }
+        }
+      }
+      break;
+  }
+}
+
 /******************************************************************************
  * Ubercart Hooks                                                             *
  ******************************************************************************/

