- I have the
Payment
andShipping service
block in the same checkout pane. - I update the Shipping service block with AJAX when selecting a Payment method, as available Shipping services depend on the selected Payment method.
But once the Shipping service block is reloaded, if I select another Shipping service and submit the form, I will be redirected to the initial pane as if my selection was unvalid - with no error message.
This is the code I use to update the Shipping services block, after saving the order to update the available Shipping services. (Inspired by some other posts)
And I can't find a way to make the Shipping selection work on submit after being reloaded via AJAX.
- Should I also update the form state?
commerce_shipping_service_rate_order($service, $order)
didn't change anything- Nor
commerce_shipping_pane_service_details_refresh($form, $form_state)
Would someone have an idea what could go wrong?
/**
* Alter checkout shipping form.
*/
function mymodule_checkout_shipping_alter(&$form, &$form_state) {
// Replace AJAX callback by a custom one.
$form['commerce_payment']['payment_method']['#ajax']['callback'] = 'mymodule_payment_pane_checkout_form_details_refresh';
}
/**
* Checkout shipping pane AJAX callback function.
*/
function mymodule_payment_pane_checkout_form_details_refresh($form, &$form_state) {
// Commands initialization.
$commands = array();
// Load the current order
$order_id = $form_state['order']->order_id;
$order = commerce_order_load($order_id);
// Set the newly selected payment option to the order
$order->data['payment_method'] = $form['commerce_payment']['payment_method']['#default_value'];
// Refresh the current order (more specific: update it's line items
commerce_cart_order_refresh($order);
// save the refreshed order (more specific: recalculate the order total and save it)
commerce_order_save($order);
// Default Commerce AJAX callback for the payment pane.
$commerce_wrapper = '#' . $form_state['triggering_element']['#ajax']['wrapper'];
$commerce_output = commerce_payment_pane_checkout_form_details_refresh($form, $form_state);
$commands[] = ajax_command_replace($commerce_wrapper, render($commerce_output));
// Update Commerce shipping pane.
$panes = commerce_checkout_panes();
$shipping_pane = drupal_get_form('commerce_shipping_pane_checkout_form', $panes['commerce_shipping'], $order);
$form['commerce_shipping']['shipping_rates'] = $shipping_pane['shipping_rates'];
$form['commerce_shipping']['shipping_service'] = $shipping_pane['shipping_service'];
$form['commerce_shipping']['service_details'] = $shipping_pane['service_details'];
$commands[] = ajax_command_replace('#commerce-shipping-service-ajax-wrapper', drupal_render($form['commerce_shipping']));
return array('#type' => 'ajax', '#commands' => $commands);
}