You are here

function commerce_stock_form_after_build in Commerce Stock 7

Same name and namespace in other branches
  1. 7.2 commerce_stock.module \commerce_stock_form_after_build()

Form after_build handler: validate the product is in stock.

1 string reference to 'commerce_stock_form_after_build'
commerce_stock_form_alter in ./commerce_stock.module
Implements hook_form_alter().

File

./commerce_stock.module, line 226
Allow commerce products to have stock levels associated with their SKU

Code

function commerce_stock_form_after_build($form, &$form_state) {
  $prod_id = $form['product_id']['#value'];
  if (isset($form['product_id']['#stock_enabled']) && isset($form['product_id']['#stock_enabled'][$prod_id]) && $form['product_id']['#stock_enabled'][$prod_id]) {
    if (isset($form['product_id']['#stock']) && isset($form['product_id']['#stock'][$prod_id])) {
      $prod_stock = $form['product_id']['#stock'][$prod_id];
      if ($prod_stock <= 0) {

        // remove the add to cart button
        $form['submit']['#access'] = FALSE;

        // remove quantity if enabled
        if (isset($form['submit'])) {
          $form['quantity']['#access'] = FALSE;
        }
      }
    }
  }
  return $form;
}