function commerce_shipping_pane_checkout_form in Commerce Shipping 7
Same name and namespace in other branches
- 7.2 includes/commerce_shipping.checkout_pane.inc \commerce_shipping_pane_checkout_form()
Checkout pane callback: builds a shipping quote selection form.
File
- includes/
commerce_shipping.checkout_pane.inc, line 12 - Callback functions for the shipping module's checkout panes.
Code
function commerce_shipping_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
$pane_form = array();
// TODO, before we active shipping methods etc, we should check that there
// actually are shippable products. Right not that is not possible because
// physical products aren't defined yet.
// Invoke the shipping methods event that will populate the order with
// an array of method IDs for available shipping methods.
$order->commerce_shipping_methods = array();
rules_invoke_all('commerce_shipping_methods', $order);
// Generate an array of shipping method options for the checkout form.
$options = array();
foreach ($order->commerce_shipping_methods as $instance_id => $method_info) {
$plugin = commerce_shipping_method_instance_load($instance_id);
$class = new $plugin['handler']['class']($plugin['settings'], $order);
if ($class
->form_label()) {
$options[$instance_id] = $class
->form_label();
}
else {
$plugin_title = isset($plugin['display_title']) ? $plugin['display_title'] : $plugin['title'];
$options[$instance_id] = !empty($plugin['shipping_label']) ? $plugin['shipping_label'] : $plugin_title;
}
}
// If no shipping methods were found, return the empty form.
if (empty($options)) {
return $pane_form;
}
// Store the shipping methods in the form for validation purposes.
$pane_form['shipping_methods'] = array(
'#type' => 'value',
'#value' => $order->commerce_shipping_methods,
);
// If at least one shipping option is available...
if (!empty($options)) {
// Add a radio select widget to specify the shipping method.
$pane_form['shipping_method'] = array(
'#type' => 'radios',
'#options' => $options,
'#ajax' => array(
'callback' => 'commerce_shipping_pane_checkout_form_details_refresh',
'wrapper' => 'shipping-details',
),
);
// Find the default shipping method using either the preselected value stored
// in the order / checkout pane or the first available method.
$pane_values = !empty($form_state['values'][$checkout_pane['pane_id']]) ? $form_state['values'][$checkout_pane['pane_id']] : array();
if (isset($pane_values['shipping_method']) && isset($options[$pane_values['shipping_method']])) {
$default_value = $pane_values['shipping_method'];
}
elseif (isset($order->data['shipping_method']) && isset($options[$order->data['shipping_method']])) {
$default_value = $order->data['shipping_method'];
}
else {
reset($options);
$default_value = key($options);
}
// Set the default value for the shipping method radios.
$pane_form['shipping_method']['#default_value'] = $default_value;
$pane_form['shipping_details'] = array();
// Store the invoked plugin class of the selected shipping method for
// later use.
$plugin = commerce_shipping_method_instance_load($pane_form['shipping_method']['#default_value']);
$class = new $plugin['handler']['class']($plugin['settings'], $order);
$pane_form['shipping_details'] = $class
->submit_form($pane_values, $checkout_pane);
$pane_form['commerce_shipping_plugin'] = array(
'#type' => 'value',
'#value' => $class,
);
$pane_form['shipping_details']['#prefix'] = '<div id="shipping-details">';
$pane_form['shipping_details']['#suffix'] = '</div>';
}
return $pane_form;
}