one simple approach

Joined: 07/23/2008
Juice: 106

Here's a simple way to display stock level for nodes, and also to remove the "add to cart" button if the stock is zero.

Add the following to the template.php file in your sites/all/themes/YOUR_THEME_NAME directory, changing the function name according to your theme's name:

function YOUR_THEME_NAME_uc_product_add_to_cart( $node ) {
  $stocklevel = uc_stock_level($node->model);
  if (is_numeric($stocklevel)) {
    // Stock tracking is active
    if ($stocklevel == 0) {
      return( '<div class="out_of_stock">' . t('Sorry, this item is out of stock') . '</div>' );
    } else {
      return( '<div class="stock_level">' . t('Number available: ') . $stocklevel . '</div>' ) . theme_uc_product_add_to_cart($node);
    }
  } else {
    // Stock tracking is not being used for this product, just show the add to cart button as normal
    return theme_uc_product_add_to_cart($node);
  }
}

Note that there are some obvious limitations to this:
1. It doesn't stop someone adding a quantity to their cart that is greater than the stock available
2. It doesn't update in real time so you may see the "add to cart" button but there may be no stock available by the time you click it
3. Stock is normally updated when an item is purchased, but this code tests at "add to cart" time
4. It is unlikely to work with product attributes. I don't use attributes, so didn't test it, but from what I understand about them, I think this approach is too simplistic.

Stock level on catalog page By: opa001 (28 replies) Mon, 03/31/2008 - 15:11