function commerce_stock_form_commerce_cart_validate in Commerce Stock 7.2
Same name and namespace in other branches
- 7 commerce_stock.module \commerce_stock_form_commerce_cart_validate()
Form validation handler for views_form_commerce_cart_form_default().
Checks each line item to make sure that they have not requested more items than are in stock.
1 string reference to 'commerce_stock_form_commerce_cart_validate'
- commerce_stock_form_alter in ./
commerce_stock.module - Implements hook_form_alter().
File
- ./
commerce_stock.module, line 218 - Commerce Stock module.
Code
function commerce_stock_form_commerce_cart_validate($form, &$form_state) {
$line_item_index = array_keys($form_state['line_items']);
if (isset($form_state['input']['edit_quantity'])) {
$products = array();
foreach ($form_state['values']['edit_quantity'] as $index => $qty) {
$line_item = $form_state['line_items'][$line_item_index[$index]];
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
if (in_array($line_item_wrapper
->getBundle(), commerce_product_line_item_types())) {
$product_id = $line_item_wrapper->commerce_product->product_id
->value();
$products[$product_id]['qty'] = isset($products[$product_id]) ? $products[$product_id]['qty'] + $qty : $qty;
$products[$product_id]['line'] = $index;
}
}
foreach ($products as $product_id => $product) {
$prod = commerce_product_load($product_id);
// Check using rules.
commerce_stock_check_product_checkout_rule($prod, $product['qty'], $stock_state, $message);
// @todo: TEST and update error structure.
if ($stock_state == 1) {
form_set_error('edit_quantity][' . $product['line'], $message);
}
elseif ($stock_state == 2) {
drupal_set_message($message);
}
}
}
}