From 8b63dc4d55affe4bb9d7de8a14e8b833a8665ac9 Mon Sep 17 00:00:00 2001
From: guy_schneerson <guy@blue-bag.com>
Date: Wed, 22 Jun 2011 16:13:01 +0100
Subject: [PATCH] cart button fixed

---
 commerce_stock.module |   42 +++++++++++++++++++++++++++++++++---------
 1 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/commerce_stock.module b/commerce_stock.module
index 998fbfb..0243704 100644
--- a/commerce_stock.module
+++ b/commerce_stock.module
@@ -34,10 +34,12 @@ function commerce_stock_menu() {
  */
 function commerce_stock_form_alter(&$form, &$form_state, $form_id){
   if (strpos($form_id, "commerce_cart_add_to_cart_form") === 0) {
+    
     $stock = array();
-    // Check to see if product has attributes.
-    // @todo: We don't have to do any of this do we?
-    if(isset($form['product_id']['#options']) ){
+    // Check to see if product has options (multiple products using
+    // the default dropdown)
+    if(isset($form['product_id']['#options']) ){  
+      $form['#after_build'][] = 'commerce_stock_form_after_build';
       $options = $form['product_id']['#options'];
       foreach ($options as $key=>$value) {
         $product = commerce_product_load($key);
@@ -50,12 +52,13 @@ function commerce_stock_form_alter(&$form, &$form_state, $form_id){
       }
       $form['product_id']['#element_validate'] = array ('commerce_stock_option_validate');
       $form['product_id']['#stock'] = $stock;
-
+      // @todo: should really be $form['product_id']['#stock'] = $stock[$key];
+      // Code will need updating in all places ['#stock'] is used
     }
-    // Product has options.
+    // A single product or uses attributes (like colour & size).
     else {
       $product = commerce_product_load($form['product_id']['#value']);
-
+      
       if(isset($product->commerce_stock['und']) ){
         if($product->commerce_stock['und'][0]['value'] <= 0){
           $form['submit']['#value'] = t('Out of Stock');
@@ -66,6 +69,23 @@ function commerce_stock_form_alter(&$form, &$form_state, $form_id){
   }
 }
 
+
+
+/**
+ * Form after_build handler: validate the product is in stock.
+ */
+function commerce_stock_form_after_build($form, &$form_state) {
+  $prod_id = $form['product_id']['#value']; 
+  if ( isset($form['product_id']['#stock']) && isset($form['product_id']['#stock'][$prod_id]) ) {   
+    $prod_stock = $form['product_id']['#stock'][$prod_id];      
+    if(isset($prod_stock) && ($prod_stock < 1) ){
+      // remove the add to stock button
+      $form['submit']['#access'] = FALSE;
+    } 
+    return $form;
+  }
+}
+
 /**
  * Implements hook_form_FORM_ID_alter().
  *
@@ -109,9 +129,13 @@ function commerce_stock_option_validate($element, &$form_state) {
       $stock[$key] = $product->commerce_stock['und'][0]['value'];
     }
   }
-
-  if ($form_state['values']['quantity'] > $element['#stock'][$form_state['values']['product_id']]) {
-    form_error($element, t('Product is out of Stock'));
+  
+  $stock = $element['#stock'][$form_state['values']['product_id']];
+  if ($element['#stock'][$form_state['values']['product_id']] < 1) {
+    form_error($element, t('Product is out of stock'));
+  }
+  else if ($form_state['values']['quantity'] > $stock) {
+    form_error($element, t("you can only order a maximum of $stock for this item"));
   }
 }
 
-- 
1.7.3.1.msysgit.0

