You are here

function commerce_checkout_router in Commerce Core 7

Redirects invalid checkout attempts or displays the checkout form if valid.

1 string reference to 'commerce_checkout_router'
commerce_checkout_menu in modules/checkout/commerce_checkout.module
Implements hook_menu().

File

modules/checkout/includes/commerce_checkout.pages.inc, line 12
The page and form callbacks for use in the checkout form.

Code

function commerce_checkout_router($order, $checkout_page = NULL) {

  // Ensure this function is routing the latest revision of the given order in
  // case some hook invoked before this page callback is executed updated some
  // value on the order that would impact routing.
  if (!commerce_order_is_latest_revision($order)) {
    entity_get_controller('commerce_order')
      ->resetCache(array(
      $order->order_id,
    ));
    $order = commerce_order_load($order->order_id);
  }
  $checkout_pages = commerce_checkout_pages();

  // If no checkout page is specified, default to the first one.
  if (empty($checkout_page)) {
    $checkout_page = reset($checkout_pages);
  }

  // If the user does not have access to checkout the order, return a 404. We
  // could return a 403, but then the user would know they've identified a
  // potentially valid checkout URL.
  if (!commerce_checkout_access($order)) {
    return MENU_NOT_FOUND;
  }

  // If the user is attempting to access an inaccessible page for their order,
  // redirect them to the proper page.
  if (!commerce_checkout_page_access($checkout_page, $order)) {
    $target_uri = commerce_checkout_order_uri($order);

    // Only redirect if the target page is different from the page the user was
    // trying to access. Otherwise give a 403 error.
    if (!empty($target_uri) && $target_uri !== $_GET['q']) {
      drupal_goto($target_uri);
    }
    else {
      return MENU_ACCESS_DENIED;
    }
  }

  // Ensure the order can proceed to checkout; if not, redirect away.
  if (!commerce_checkout_order_can_checkout($order)) {
    drupal_goto('<front>');
  }

  // Prior to displaying the checkout form, allow other modules to route the
  // checkout form.
  module_invoke_all('commerce_checkout_router', $order, $checkout_page);

  // Update the page title if specified.
  if (!empty($checkout_page['title'])) {
    drupal_set_title($checkout_page['title']);
  }
  return drupal_get_form('commerce_checkout_form_' . $checkout_page['page_id'], $order, $checkout_page);
}