--- uc_attribute_stock_filter.module 2009-04-13 20:35:56.000000000 -0400 +++ uc_attribute_stock_filter.module.patched 2009-04-13 20:45:11.000000000 -0400 @@ -15,40 +15,41 @@ * Implementation of hook_nodeapi(). */ function uc_attribute_stock_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { - switch ($op) { - case 'view': - // Get attribute count for this product - $attrib = db_fetch_object(db_query("SELECT COUNT(aid) AS count FROM {uc_product_attributes} WHERE nid = %d", $node->nid)); + static $available = array(); - // Only proceed if we have more than one attribute (otherwise we do the filtering with hook_form_alter()) - if ($attrib->count > 1) { - // Grab serialized options and stock levels - $result = db_query("SELECT a.combination AS combination, s.stock AS stock FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1", $node->nid); - - // This builds an array where each option is a key containing its available combinations as elements - $available = array(); - while ($data = db_fetch_object($result)) { - $combo = unserialize($data->combination); - if ($data->stock > 0) { // Stock is available - foreach ($combo as $key => $option) { // Grab possible combinations - foreach ($combo as $avail_key => $avail_opt) { - if ($avail_key != $key) { - $available[$option][] = $avail_opt; - } + if (($op == 'view') || + (($op == 'load') && (preg_match('%^/catalog/%', request_uri())))) { + + // Get attribute count for this product + $attrib = db_fetch_object(db_query("SELECT COUNT(aid) AS count FROM {uc_product_attributes} WHERE nid = %d", $node->nid)); + + // Only proceed if we have more than one attribute (otherwise we do the filtering with hook_form_alter()) + if ($attrib->count > 1) { + // Grab serialized options and stock levels + $result = db_query("SELECT a.combination AS combination, s.stock AS stock FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1", $node->nid); + + // This builds an array where each option is a key containing its available combinations as elements + while ($data = db_fetch_object($result)) { + $combo = unserialize($data->combination); + if ($data->stock > 0) { // Stock is available + foreach ($combo as $key => $option) { // Grab possible combinations + foreach ($combo as $avail_key => $avail_opt) { + if ($avail_key != $key) { + $available[$node->nid][$option][] = $avail_opt; } } } } - if (!empty($available)) { // If there are no stock levels at all, we assume stocks aren't used and skip filtering. + } + if (!empty($available)) { // If there are no stock levels at all, we assume stocks aren't used and skip filtering. - // Encode the array as JSON and add the JavaScript - $js_array = "var uc_asf_AvailableOptions = ". json_encode($available); - drupal_add_js($js_array, 'inline'); - drupal_add_js(drupal_get_path('module', 'uc_attribute_stock_filter') .'/uc_attribute_stock_filter.js', 'module'); - } + // Encode the array as JSON and add the JavaScript + $js_array = "var uc_asf_AvailableOptions = ". json_encode($available); + drupal_add_js($js_array, 'inline'); + drupal_add_js(drupal_get_path('module', 'uc_attribute_stock_filter') .'/uc_attribute_stock_filter.js', 'module'); } - break; + } } } @@ -85,7 +86,7 @@ function uc_attribute_stock_filter_form_ } } else if ($product_attributes->count > 0) { // The product has at least one attribute - $result = db_query("SELECT a.combination AS combination, s.stock AS stock FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1", arg(1)); + $result = db_query("SELECT a.combination AS combination, s.stock AS stock FROM {uc_product_adjustments} a, {uc_product_stock} s WHERE a.nid = %d AND s.sku = a.model AND s.active = 1", $form['nid']['#value']); // This builds an array containing the available options $available = array(); @@ -108,6 +109,5 @@ function uc_attribute_stock_filter_form_ } } } - } -} \ No newline at end of file +}