You are here

function commerce_stock_checkout_validate in Commerce Stock 7.2

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

Common stock validation function.

2 calls to commerce_stock_checkout_validate()
commerce_stock_checkout_form_validate in ./commerce_stock.module
Form validation handler for commerce_checkout_form_checkout().
commerce_stock_commerce_checkout_pane_checkout_form in ./commerce_stock.module
Form constructor for the stock checkout pane form.

File

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

Code

function commerce_stock_checkout_validate($order_wrapper) {
  $found_errors = FALSE;

  // Check each line item.
  foreach ($order_wrapper->commerce_line_items as $index => $line_item_wrapper) {
    if (in_array($line_item_wrapper
      ->getBundle(), commerce_product_line_item_types())) {
      $product_id = $line_item_wrapper->commerce_product->product_id
        ->value();
      $product = commerce_product_load($product_id);
      $qty_ordered = commerce_stock_check_cart_product_level($product_id);

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

      // @todo: TEST and update error structure
      if ($stock_state == 1) {
        form_set_error("stock_error", $message);
        $found_errors = TRUE;
      }
      elseif ($stock_state == 2) {
        drupal_set_message($message);
      }
    }
  }

  // If out of stock items send back to the cart form.
  if ($found_errors) {
    drupal_set_message(t('Please adjust quantities before continuing to checkout.'));
    $cart_url = url('cart', array(
      'absolute' => TRUE,
    ));
    drupal_goto($cart_url);
  }
}