You are here

function uc_cart_checkout_form_validate in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_cart/uc_cart.module \uc_cart_checkout_form_validate()
  2. 7.3 uc_cart/uc_cart.pages.inc \uc_cart_checkout_form_validate()

Form validation for uc_cart_checkout_form().

See also

uc_cart_checkout_form()

uc_cart_checkout_form_submit()

File

uc_cart/uc_cart.pages.inc, line 280
Cart menu items.

Code

function uc_cart_checkout_form_validate($form, &$form_state) {
  global $user;
  if (empty($_SESSION['cart_order'])) {
    $order = uc_order_new($user->uid);
    $_SESSION['cart_order'] = $order->order_id;
  }
  else {
    $order = uc_order_load($_SESSION['cart_order']);
  }
  db_query("DELETE FROM {uc_order_products} WHERE order_id = %d", $order->order_id);
  $order->products = unserialize($form_state['values']['cart_contents']);
  $context = array(
    'revision' => 'original',
    'type' => 'order_product',
  );
  foreach ($order->products as $key => $item) {
    $price_info = array(
      'price' => $item->price,
      'qty' => $item->qty,
    );
    $context['subject'] = array(
      'order' => $order,
      'product' => $item,
      'node' => node_load($item->nid),
    );

    // Get the altered price per unit, as ordered products have a locked-in
    // price. Price altering rules may change over time, but the amount paid
    // by the customer does not after the fact.
    $price = uc_price($price_info, $context) / $item->qty;
    if ($order->products[$key]->price != $price) {
      $order->products[$key]->data['altered_price'] = $price;
    }
  }
  $order->order_total = uc_order_get_total($order, TRUE);

  // Validate/process the cart panes.  A FALSE value results in failed checkout.
  $_SESSION['checkout_valid'] = TRUE;
  foreach (element_children($form_state['values']['panes']) as $pane_id) {
    $func = _checkout_pane_data($pane_id, 'callback');
    if (is_string($func) && function_exists($func)) {
      $isvalid = $func('process', $order, $form_state['values']['panes'][$pane_id]);
      if ($isvalid === FALSE) {
        $_SESSION['expanded_panes'][] = $pane_id;
        $_SESSION['checkout_valid'] = FALSE;
      }
    }
  }
  $order->line_items = uc_order_load_line_items($order, TRUE);
  uc_order_save($order);
}