You are here

function commerce_stock_add_to_cart_validate in Commerce Stock 7.2

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

Form validation handler for commerce_cart_add_to_cart_form().

For product display with one product or attributes. Validates the product and quantity to add to the cart. Also checks if the add to cart form should be enabled (in stock).

See also

commerce_cart_add_to_cart_form()

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

File

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

Code

function commerce_stock_add_to_cart_validate($form, &$form_state) {
  if ($form_state['submitted']) {

    // Get product and quantity.
    $qty = $form_state['values']['quantity'];
    $product_id = $form_state['values']['product_id'];
    $product = commerce_product_load($product_id);
    $qty_ordered = commerce_stock_check_cart_product_level($product_id);

    // Check using rules.
    commerce_stock_check_product_rule($product, $qty, $qty_ordered, $stock_state, $message);

    // Action.
    if ($stock_state == 1) {
      form_set_error("stock_error", $message);
    }
    elseif ($stock_state == 2) {
      $form_state['values']['quantity'] = $qty;
      drupal_set_message($message);
    }
  }
}