Display a message when a product is out of stock

Posts: 47
Joined: 01/20/2008
Bug Finder

I want to be able to display a message to my users when a product is out of stock. That way, when they place an order for an out-of-stock product, they will be aware of the possibility that order may not be fulfilled.

After searching through the forums, I concluded that the best way of doing this would be to add a snippet of PHP to my to my template.

The following:

<?php
print uc_stock_level($node->model);
?>
will detect the stock levels, but I’m not sure how to write an if statement that will display a message if the stock level is less than 0.

Does anyone know of a solution to my problem (or a good resource that explains how to write PHP IF statements that doesn’t require a knowledge of PHP to understand).

[I know that there is a contributed module, Simple Stock Levels, that does this. However, at the bottom of the page [http://www.ubercart.org/contrib/132], there is a message saying that it is not a good idea to use this module with the updated ubercart].

Thanks ^_^

Posts: 5245
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

At its most basic, you can try something like this:

<?php
if (uc_stock_level($node->model) <= 0) {
 
drupal_set_message('This product is out of stock.', 'error');
}
?>

You can adjust the error message, display it some other way, or do whatever you want... but that's a basic if statement for ya. If you just want it to be a normal message, take out the ", 'error'".