function uc_stock_notify_form_alter(&$form, $form_state, $form_id) {
    if (strstr($form_id, 'uc_product_add_to_cart_form_') || strstr($form_id, 'uc_catalog_buy_it_now_form_')) {
    // init vars
    $count = 0;
    $active = 0;
         // Does the product have attributes?
         $product_attributes = db_query("SELECT COUNT(nid) as count FROM {uc_product_adjustments} WHERE nid = :nid", array(':nid'=>$form['nid']['#value']));
         $product_attributes = $product_attributes->fetch();
         if ($product_attributes->count < 1) {
           $stock = db_query("SELECT s.stock as count, s.active FROM {uc_product_stock} s WHERE s.nid = :nid", array(':nid'=>$form['nid']['#value']));
           $stock = $stock->fetch();
           $count = $stock->count;
         } else {
           $result = db_query("SELECT a.combination AS combination, s.stock AS stock, s.active FROM {uc_product_adjustments} a LEFT JOIN {uc_product_stock} s ON a.model = s.sku WHERE a.nid = :nid", array(':nid'=>$form['nid']['#value']));
           foreach ($result as $data) {
           if(!$active){
           	$active = $data->active;  // looking for one active sku...
           }
           $count = $count + $data->stock;
           }           
        }
        if ($result = db_query("SELECT sku,stock, threshold FROM {uc_product_stock} WHERE nid = :nid AND active = :active", array(':nid' => $form['nid']['#value'], ':active' => 1))->fetch()) {
            $model = $result->sku;
        }
            if ($count < 1 && $active) {
                // product is out of stock
                unset($form['qty']);
                unset($form['#submit']);
                unset($form['actions']['submit']);
                unset($form['#validate']);

                $form['model'] = array(
                    '#type' => 'value',
                    '#value' => $model,
                );
                $form['actions']['notify'] = array(
                    '#type' => 'submit',
                    '#attributes' => array('class' => array('stock-notify')),
                    '#value' => t('Notify when back in stock'),
                );

                $form['#submit'] = array('uc_stock_notify_form_submit');
            }
        }
    
}

function uc_stock_notify_uc_add_to_cart($nid, $qty, $data) {
    if ($result = db_query("SELECT sku,stock, threshold FROM {uc_product_stock} WHERE nid = :nid AND active = :active", array(':nid' => $nid, ':active' => 1))->fetch()) {
        if ($result->stock - $qty < 0) {
            $value[] = array(
                'success' => FALSE,
                'message' => t('Sorry, this kind of product out of stock.'),
                'silent' => FALSE
            );
            return $value;
        }
    }
}
