A small and quick fix
I spent some time debugging and did some research, here is a small and quick fix that did the trick. I added this in my theme functions.php file, but it can be added in a plugin also.
add_filter( 'woocommerce_get_script_data', 'fix_ajax_handler_translation' ); if ( ! function_exists( 'fix_ajax_handler_translation' ) ) { /** * Fix the lost translations on AJAX calls made in WordPress >= 5.0 with WooCommerce >= 3.5 * on checkout pages for example. * * @param array $params Script data. * @return array */ function fix_ajax_handler_translation( $params ) { $locale = determine_locale(); $lang = ( ! empty( $locale ) ) ? strstr( $locale, '_', true ) : ''; if ( empty( $lang ) ) { // Fail-fast, no need to check further. Fallback to the defaults. return $params; } if ( isset( $params['wc_ajax_url'] ) ) { $params['wc_ajax_url'] = $params['wc_ajax_url'] . '&lang=' . $lang; } return $params; } }
Please note that if you use WordPress < 5.0 the native function determine_locale
does not exist (it was introduced with 5.0), but you can use it as an alternative for get_user_locale
.
I hope this helps anyone that encounters the same issue.
If you find the article useful and would like to support my work, please consider making a donation / buy me a coffee, or share this article on your feed.
Thank you very much!