When a user enters in a quantity to add to their cart, I am trying to check that quantity against the stock level of that product, and when the stock is less than their quantity, it returns and error. This is as far as I have gotten, and I am stuck. Got any ideas?

function uc_check_stock_form_alter(&$form,&$form_state,$form_id) {
  if ($form_id == 'uc_product_add_to_cart_form_') {
    $form['#validate'][] = 'uc_check_stock_form_validate';
  return $form;

function uc_check_stock_form_validate($form, &$form_state) {

  $stock = db_result(db_query("SELECT `stock` FROM `uc_product_stock` WHERE `nid` = '%d'", $form_state['values']['node']->nid));
  if (is_numeric($stock) && $stock < $form_state['values']['qty']) {
  form_set_error('qty', t('Quantity is greater than our available stock. Please adjust your quantity for the this item.'));

1 Answer 1


Someone created a module for that already, needs some fixing IMHO, maybe you can play with that and make something better, I'll definitely be very thankful...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.