You are here

function commerce_stock_checkout_validate in Commerce Stock 7

Same name and namespace in other branches
  1. 7.2 commerce_stock.module \commerce_stock_checkout_validate()
2 calls to commerce_stock_checkout_validate()
commerce_stock_checkout_form_validate in ./commerce_stock.module
Form validate handler: validate checkout form.
commerce_stock_commerce_checkout_pane_checkout_form in ./commerce_stock.module
The stock checkout pane form: call stock the validation when displaying the form this will alow us to redirect the user before he starts checkout

File

./commerce_stock.module, line 179
Allow commerce products to have stock levels associated with their SKU

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);
      $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
      if (!(isset($product_wrapper->commerce_stock_override) && $product_wrapper->commerce_stock_override
        ->value() == 1)) {
        $desired_purchase = $line_item_wrapper->quantity
          ->value();
        if (commerce_stock_product_check_out_of_stock($product_id, $desired_purchase, $remaining_stock)) {
          form_set_error("out_of_stock_{$index}", t('The maximum quantity of %title that can be purchased is %max.', array(
            '%title' => $product->title,
            '%max' => $remaining_stock,
          )));
          $found_errors = TRUE;
        }
      }
    }
  }

  // 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);
  }
}