Definition

hook_checkoutapi(&$txn, $op, $arg3 = NULL, $arg4 = NULL)
ecommerce-4-7/docs/developer/hooks/core.php, line 66

Description

Manipulate the checkout process, including injecting form pages.

Checkoutapi can be implemented by any module. It can be used to insert a page into into the checkout process and even validate/save the form data. Another feature of the API is the ability to push data onto the final review page before final checkout. Note that the order of the form pages is controlled via http://example.com/index.php?q=admin/store/checkout

Parameters

&$txn The transaction object for an order. This keeps growing in data from screen to screen.

$op What kind of action is being performed. Possible values:

  • "form": Inject a form page into the checkout process. Don't forget to add a submit button.
  • "save": The injected form page has been submitted. Save your data in this hook. IMPORTANT: $txn->screen must be incremented here in order to go to the next screen! $txn->screen++;
  • "validate": The customer has just finished editing the form page and is trying to submit it. This hook can be used to check or even modify the transaction object. Errors should be set with form_set_error().
  • "review": The last page of the checkout process is being viewed before the order is placed.
  • "review_validate": validation for review page. Called within the form validate hook so has same requirements.
  • "review_save": save section of the review page. Called within the form submit hook and has same requirements.
$arg3
  • Optional parameter to pass along.
$arg4
  • Optional parameter to pass along.

Return value

This varies depending on the operation.

  • The "save" and "validate" operations have no return value.
  • The "form" returns a form array to build the modules checkout screen.
  • The "review" operation should return a form array. The themeing is done by theme_hook_review_form
  • The "review_validate" operations have no return value.
  • The "review_submit" operations have no return value.

Code

<?php
function hook_checkoutapi(&$txn, $op, $arg3 = NULL, $arg4 = NULL) {
  $output = '';
  switch ($op) {
    case 'form':
      if ($form = payment_view_methods()) {
        drupal_set_title(t('Please select a payment method'));
        $form[] = array(
          '#type' => 'submit',
          '#value' => t('Continue'),
          );
        return $form;
      }
      else {
        foreach (payment_get_methods() as $module) {
          if (module_invoke($module, 'paymentapi', $edit, 'display name')) {
            $txn->payment_method = $module;
            break;
          }
        }
        return false;
      }

    case 'validate':
      if (!$txn->payment_method) {
        form_set_error('payment_method', t('Please choose a payment method.'));
      }
      break;

    case 'save':
      $txn->screen++;
      break;

    case 'review':
      $form['payment'] = array('#value' => module_invoke($txn->payment_method, 'paymentapi', $txn, 'display name'));
      return $form;

    case 'review_validate':
      break;

    case 'review_save':
      break;
  }
}
?>