if you have more then one product for a product display then the button disable code does not run.

This is becouse the disabeling code is in the else of “if(isset($form['product_id']['#options']) ){“ in the form_alter.

I fixed this on my copy by using an after_build function

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']) ){      
      $form['#after_build'][] = 'commerce_stock_form_after_build';
      .....


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) ){
     $form['submit']['#access'] = FALSE;
   } 
 }
  return $form;
}

One issue i had was that the $form['submit']['#disabled'] dose not work in the after_build, but i think it is better to hide the button, this will also resolve the Text instead of a disabled button.

one other small observetion the line:
$form['product_id']['#stock'] = $stock;
asignes the array of the items to each item so product 1 will have an array of stock levels for products 1...x, this is a bit waistfull and makes for harder coding in the validation functions.
i think that using the line
$form['product_id']['#stock'] = $stock[$key];
should work better as the actual stock count is assigned to the product

Please let me know if you need more information or help, i can send you my changed unit if you wish or try and figure out how to build a patch (haven't got around to it using Git mutch)

Comments

rfay’s picture

Yes, it would be the right thing for you to provide a patch. Please learn how - it's easy. http://drupal.org/node/707484

guy_schneerson’s picture

thanks rfay did figure it out the other day, easy realy will provide a patch soon

guy_schneerson’s picture

StatusFileSize
new3.26 KB

This patch fixes two issues related to the form behaviour of product displays with multiple products (using the dropdown).

• Add to cart button is removed if out of stock
• If stock exists and the user adds more items than there are in stock a new message "you can only order a maximum of x for this item" instead of "Product is out of stock"

Note that the other case is when you only have one product or are using attributes to select your product like colour and size, if you have the quantity box enabled you can add a product with a quantity larger than the stock.

I'll add a separate issue for this use case and try and look at it at a later stage.

guy_schneerson’s picture

rfay please let me know if the patch worked

rfay’s picture

Status: Active » Needs review

Thanks for the patch. Setting to "needs review", meaning to other users as well as the maintainer that there's a patch that needs reviewing. Congratulations on this step!

guy_schneerson’s picture

Assigned: Unassigned » guy_schneerson
Status: Needs review » Needs work

i am updating and reviewing all my recent patches

guy_schneerson’s picture

StatusFileSize
new3.16 KB

updated and tested patch

Also disables the Quantity widget if it is enabled

One issue with this is that the approach for the product dropdown is not the same as for the other cases like attributes. Those two should be unified at some point, ill raise an new issue for this.

guy_schneerson’s picture

Status: Needs work » Needs review
guy_schneerson’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.