How to properly add a Commerce Shipping Service option via AJAX during checkout

  • I have the Payment and Shipping 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); } 
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How to properly add a Commerce Shipping Service option via AJAX during checkout

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.