You are here

function commerce_stock_form_after_build in Commerce Stock 7.2

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

Form after_build handler: Validates that the product is in stock.

File

./commerce_stock.module, line 444
Commerce Stock module.

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;
}