...
 
Commits (1)
Please use the named branches like 7.x-1.x
Commerce Examples is a place for demonstrations of Drupal Commerce APIs.
If you have additional examples you'd like to contribute, please provide them
as patches in the issue queue at
http://drupal.org/project/issues/commerce_examples.
\ No newline at end of file
name = Checkout Pane Example
description = Demonstrates providing an additional checkout pane
package = Commerce (contrib)
dependencies[] = commerce_checkout
core = 7.x
<?php
/**
* @file checkout_pane_example.install
*/
/**
* Implements hook_uninstall().
*
*/
function checkout_pane_example_uninstall() {
variable_del('checkout_pane_example_request_another_email');
}
<?php
/**
* @file checkout_pane_example.module
* Demonstrates the addition of a new pane to the checkout system.
*/
/**
* Implements hook_menu().
*
* Simply presents a page that will explain what this module is for.
* hook_menu() has nothing to do with the checkout pane functionality.
*/
function checkout_pane_example_menu() {
$items['commerce_examples/checkout_pane_example'] = array(
'title' => 'Pane Example',
'page callback' => 'checkout_pane_example_info_page',
'access callback' => TRUE,
);
return $items;
}
/**
* This function simply returns information about the example to help the
* user understand its purpose.
*/
function checkout_pane_example_info_page() {
return array(
'#markup' => t('The pane example adds an extra "Pane Example" pane to the checkout system. It also demonstrates how to alter the panes other modules have provided.'),
);
}
/**
* Implements hook_commerce_checkout_pane_info().
*
* This is where we define the name of the pane, related information, and
* the base name of all the form builder functions used to present the
* pane.
*/
function checkout_pane_example_commerce_checkout_pane_info() {
$panes['checkout_pane_example'] = array(
'title' => t('Pane Example'),
'base' => 'checkout_pane_example_pane',
// The checkout page where this should be displayed by default.
'page' => 'checkout',
'weight' => -5,
// The form functions could be found in another file...
// 'file' => 'includes/pane.inc',
);
return $panes;
}
/**
* Implements hook_commerce_checkout_pane_info_alter().
*
* Here we can change the definitions of panes that have been provided by this
* or other modules.
*
* @param $panes
* All the panes that have been defined.
*/
function checkout_pane_example_commerce_checkout_pane_info_alter(&$panes) {
if (!empty($panes['cart_contents'])) {
// Change the title of the "Cart Contents" pane.
$panes['cart_contents']['title'] = t('Your big wonderful shopping cart brimming over with shopaholic nonsense');
}
}
/**
* Account pane: settings form callback.
*
* This form provides configuration information for the pane. In this case
* we set a variable determining whether to request an additional email address.
*/
function checkout_pane_example_pane_settings_form($checkout_pane) {
$form['checkout_pane_example_request_another_email'] = array(
'#type' => 'checkbox',
'#title' => t('Request another email address.'),
'#description' => t('If checked, the form will request an additional email address.'),
'#default_value' => variable_get('checkout_pane_example_request_another_email', FALSE)
);
return $form;
}
/**
* Example Pane: form callback.
*
* This is a standard FAPI form which will be presented in the pane.
* The form gathers and stores information from $order->data, an array which
* can be populated with free-form keys and values.
*/
function checkout_pane_example_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
$form['nickname'] = array(
'#type' => 'textfield',
'#title' => t('Your nickname'),
'#description' => t("We'd like to call you by your nickname."),
'#default_value' => !empty($order->data['nickname']) ? $order->data['nickname'] : '',
'#required' => TRUE,
);
if (variable_get('checkout_pane_example_request_another_email', FALSE)) {
// Note we're not using theme_username() to avoid linking out of checkout.
$form['another_email'] = array(
'#type' => 'textfield',
'#title' => t('Another e-mail address'),
'#description' => t('Please give us a second email address.'),
'#required' => TRUE,
'#default_value' => !empty($order->data['another_email']) ? $order->data['another_email'] : '',
);
}
return $form;
}
/**
* Example pane: validation callback.
*/
function checkout_pane_example_pane_checkout_form_validate($form, &$form_state, $checkout_pane, $order) {
if (variable_get('checkout_pane_example_request_another_email', FALSE) && !empty($form_state['values'][$checkout_pane['pane_id']])) {
$pane_values = $form_state['values'][$checkout_pane['pane_id']];
// Display an error if an invalid e-mail address was given.
if ($error = user_validate_mail($pane_values['another_email'])) {
form_set_error($checkout_pane['pane_id'] . '][another_email', t('Please provide a valid e-mail address.'));
return FALSE;
}
}
return TRUE;
}
/**
* Example pane: checkout form submission callback.
*/
function checkout_pane_example_pane_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
if (!empty($form_state['values'][$checkout_pane['pane_id']])) {
$pane_values = $form_state['values'][$checkout_pane['pane_id']];
if (!empty($pane_values['another_email'])) {
$order->data['another_email'] = $pane_values['another_email'];
}
$order->data['nickname'] = $pane_values['nickname'];
commerce_order_save($order);
}
}
/**
* Example pane: presents the information we've collected in the Review checkout
* pane.
*/
function checkout_pane_example_pane_review($form, $form_state, $checkout_pane, $order) {
$content['nickname'] = array(
'#type' => 'item',
'#title' => t('Nickname'),
'#markup' => check_plain($order->data['nickname']),
);
$content['another_email'] = array(
'#type' => 'item',
'#title' => t('Another Email address'),
'#markup' => check_plain($order->data['another_email']),
);
return drupal_render($content);
}