How to check the stock count of an ubercart node
Note: here's a good page to have bookmarked when you use Ubercart with D6: http://www.ubercart.org/docs/api (as opposed to http://api.ubercart.me for when you use D7).

Also note: this ended up being a lot more in-depth than I anticipated, but I was already knee-deep in it so I figured I'd finish it up. ;) That said...



You're definitely on the right track... Like you said, you could just supply the function uc_stock_level($sku) with the value of $sku, but that might be a lot of SKUs and therefore kind of resource intensive.

Another alternative that should work would be something like this:

Change this part of your code:

$output = '&lt;ul&gt;';
// Avoid unnecessary "Invalid foreach" errors showing up in the log:
if (!empty($children)) {
    // If not empty, run the foreach loop:
    foreach ($children as $term) {
        // Then check to see if any nodes exist for that term id:
        $number_of_nodes = taxonomy_term_count_nodes($term-&gt;tid);
        // If there ARE nodes...
        if ($number_of_nodes &gt; 0) {
          // ... then add them to the output:
          $output .= '&lt;li&gt;';
          $output .= l($term-&gt;name . ' (' . $number_of_nodes . ')', 'taxonomy/term/' . $term-&gt;tid);
          $output .= '&lt;/li&gt;';
          }
    }
}
$output .= '&lt;/ul&gt;';


To something like this:

$output = '&lt;ul&gt;';
// Avoid unnecessary "Invalid foreach" errors showing up in the log:
if (!empty($children)) {
  // If not empty, run the foreach loop:
  foreach ($children as $term) {
    // Then check to see if any nodes exist for that term id:
    $number_of_nodes = taxonomy_term_count_nodes($term-&gt;tid);
    // And then check to see what the current stock level is:
    $stock_level = fetch_stock_level_from_termid($term-&gt;tid);
    // If there ARE nodes...
    if ($number_of_nodes &gt; 0) {
      // And if the product is in stock...
      if ($stock_level &gt; 0) {
        // ... then add them to the output:
        $output .= '&lt;li&gt;';
        $output .= l($term-&gt;name . ' (' . $number_of_nodes . ')', 'taxonomy/term/' . $term-&gt;tid);
        $output .= '&lt;/li&gt;';
      }
    }
  }
}
$output .= '&lt;/ul&gt;';


You'll then need to add the function fetch_stock_level_from_termid() to your code as well:

function fetch_stock_level_from_termid($term_id) {
  $sql = "SELECT SUM( {uc_product_stock}.stock ) AS stock
          FROM {uc_product_stock}
          INNER JOIN {node} ON {uc_product_stock}.nid = {node}.nid
          INNER JOIN {term_node} ON {node}.vid = {term_node}.vid
          WHERE {term_node}.tid = %d
          AND {node}.status = 1";
  $stock_available = db_result(db_query($sql, $term_id));
  return $stock_available;
}


Hope that helps... :)