You are here

function commerce_stock_cart_state_validate in Commerce Stock 7.2

Form validation handler for commerce_cart_add_to_cart_form().

Checks if the add to cart form should be enabled (in stock).

1 call to commerce_stock_cart_state_validate()
commerce_stock_form_alter in ./commerce_stock.module
Implements hook_form_alter().

File

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

Code

function commerce_stock_cart_state_validate($form_id, &$form, &$form_state) {
  $product_id = $form['product_id']['#value'];
  $product = commerce_product_load($product_id);
  $qty_ordered = commerce_stock_check_cart_product_level($product_id);

  // Initialize the form.
  $form['submit']['#value'] = $form['submit']['#value'];
  $form['submit']['#disabled'] = FALSE;
  $form['#attributes']['class']['stock'] = 'in-stock';

  // @codingStandardsIgnoreLine
  global $stock_cart_check_data;
  $stock_cart_check_data = array(
    'form' => &$form,
  );

  // Integration with rules_form_alter().
  if (module_exists('rules_form_alter')) {

    // Make sure rules_form_alter actions work from the stock event.
    $rules_form_alter_data =& drupal_static('rules_form_alter_data', array());

    // Set the form data that will be used by rules.
    $rules_form_alter_data['id'] = $form_id;
    $rules_form_alter_data['form'] =& $form;
    $rules_form_alter_data['state'] =& $form_state;
  }

  // Invoke the stock check event.
  rules_invoke_event('commerce_stock_check_add_to_cart_form_state', $product, $qty_ordered);
}