You are here

function commerce_stock_add_to_cart_validate in Commerce Stock 7

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

Form validate handler: validate checkout form.

Make sure all items in the cart are in stock

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 209
Allow commerce products to have stock levels associated with their SKU

Code

function commerce_stock_add_to_cart_validate($form, &$form_state) {
  $qty = $form_state['input']['quantity'];
  $product_id = $form_state['input']['product_id'];
  $product = commerce_product_load($product_id);
  $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
  $qty_ordered = commerce_stock_check_cart_product_level($product_id);
  $qty_total = $qty + $qty_ordered;
  if (!(isset($product_wrapper->commerce_stock_override) && $product_wrapper->commerce_stock_override
    ->value() == 1)) {
    if (commerce_stock_product_check_out_of_stock($product_id, $qty_total, $remaining_stock)) {
      form_set_error("stock_error", t('The maximum quantity of %title that can be purchased is %max.', array(
        '%title' => $product->title,
        '%max' => $remaining_stock,
      )));
    }
  }
}