Created
June 13, 2014 18:32
-
-
Save mglaman/5418a90643de5e6016df to your computer and use it in GitHub Desktop.
Adds a field to the Commerce checkout, such as a sales rep.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_commerce_checkout_pane_info(). | |
*/ | |
function module_commerce_checkout_pane_info() { | |
$panes['module_salesrep'] = array( | |
'title' => t('Sales Representative'), | |
'base' => 'module_salesrep', | |
// The checkout page where this should be displayed by default. | |
'page' => 'checkout', | |
'weight' => -19, | |
); | |
return $panes; | |
} | |
/** | |
* 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 module_salesrep_settings_form($checkout_pane) { | |
$form = array(); | |
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 module_salesrep_checkout_form($form, &$form_state, $checkout_pane, $order) { | |
$pane_form['sales_rep_name'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Sales Representative Name'), | |
'#description' => t("Please enter the name of your sales representative, if available."), | |
// If using field on user, load an order wrapper &.... $order_wrapper->uid->field_sales_rep_name->value(); | |
'#default_value' => !empty($order->data['sales_rep_name']) ? $order->data['sales_rep_name'] : '', | |
'#required' => FALSE, | |
); | |
return $pane_form; | |
} | |
/** | |
* Example pane: checkout form submission callback. | |
*/ | |
function module_salesrep_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['sales_rep_name'])) { | |
// If using a field on user, load wrapper && $order_wrapper->uid->field_sales_rep_name->set($pane...); | |
$order->data['sales_rep_name'] = $pane_values['sales_rep_name']; | |
} | |
} | |
} | |
/** | |
* Example pane: presents the information we've collected in the Review checkout | |
* pane. | |
*/ | |
function module_salesrep_review($form, $form_state, $checkout_pane, $order) { | |
if (!empty($order->data['sales_rep_name'])) { | |
$content['sales_rep_name'] = array( | |
'#type' => 'item', | |
'#title' => t('Sales Representative Name'), | |
// If using order... $order_wrapper->uid->field_sales_rep_name | |
'#markup' => check_plain($order->data['sales_rep_name']), | |
); | |
} | |
return drupal_render($content); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment