WooCommerce与WPML:自定义AJAX调用无法获取选中货币价格
问题:WooCommerce + WPML AJAX搜索仅返回默认货币价格
- 定制开发的WooCommerce商店集成WPML插件,支持多语言+对应独立货币,默认货币为EUR
- 前端切换语言时,货币及商品价格可正常切换,但自定义AJAX搜索的WP_Query中,仅能获取默认货币EUR的价格,非AJAX场景价格显示正常
- 出现问题的AJAX处理函数代码:
function do_search() { check_ajax_referer('main_nonce', 'security'); $search_query = sanitize_text_field($_POST['search_query']); $reults = array(); $args = array( 's' => $search_query, 'post_type' => 'product', 'posts_per_page' => 10, ); $query = new WP_Query($args); // Check is query not a empty string if ($search_query === '') { wp_send_json(array( 'status' => 'success', 'results' => $reults, )); return; } if ($query->have_posts()) { // Get all rezults data while ($query->have_posts()) { $query->the_post(); // $product = wc_get_product($query->post->ID); $product = wc_get_product(get_the_ID()); $price_html = $product->get_price_html(); array_push($reults, array( 'title' => get_the_title(), 'price' => $price_html, 'permalink' => get_permalink(), 'image' => get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'), )); } // Return results data wp_send_json(array( 'status' => 'success', 'results' => $reults, )); } else { // Return no results message wp_send_json(array( 'status' => 'success', 'results' => $reults, )); } wp_reset_postdata(); } add_action('wp_ajax_do_search', 'do_search'); add_action('wp_ajax_nopriv_do_search', 'do_search');
原因分析
AJAX请求默认不会携带当前用户的语言上下文,WPML无法识别当前页面的语言环境,因此会返回默认货币(EUR)的价格数据。
解决方案
1. 前端AJAX请求添加语言参数
在发送AJAX请求时,带上当前语言代码(可通过WPML全局变量ICL_LANGUAGE_CODE获取):
// 示例jQuery AJAX请求 jQuery.ajax({ url: ajaxurl, type: 'POST', data: { action: 'do_search', security: '你的nonce值', search_query: '搜索关键词', lang: ICL_LANGUAGE_CODE // 传递当前语言代码 }, success: function(response) { // 处理返回结果 } });
2. 后端AJAX函数中切换语言环境
在AJAX处理函数开头,接收语言参数并调用WPML的icl_switch_language()函数切换语言,确保WPML和WooCommerce使用当前语言对应的货币:
修改后的AJAX处理函数:
function do_search() { check_ajax_referer('main_nonce', 'security'); // 接收并验证语言参数 $current_lang = ''; if (isset($_POST['lang']) && !empty($_POST['lang'])) { $current_lang = sanitize_text_field($_POST['lang']); // 切换到当前语言环境 if (function_exists('icl_switch_language')) { icl_switch_language($current_lang); } } $search_query = sanitize_text_field($_POST['search_query']); $reults = array(); $args = array( 's' => $search_query, 'post_type' => 'product', 'posts_per_page' => 10, // WPML:确保查询当前语言的商品 'lang' => $current_lang ?? ICL_LANGUAGE_CODE, ); $query = new WP_Query($args); if ($search_query === '') { wp_send_json(array( 'status' => 'success', 'results' => $reults, )); return; } if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); $product = wc_get_product(get_the_ID()); $price_html = $product->get_price_html(); array_push($reults, array( 'title' => get_the_title(), 'price' => $price_html, 'permalink' => get_permalink(), 'image' => get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'), )); } wp_send_json(array( 'status' => 'success', 'results' => $reults, )); } else { wp_send_json(array( 'status' => 'success', 'results' => $reults, )); } wp_reset_postdata(); } add_action('wp_ajax_do_search', 'do_search'); add_action('wp_ajax_nopriv_do_search', 'do_search');
关键修改点
- 新增语言参数接收与验证,调用
icl_switch_language()切换WPML语言环境 - 在
WP_Query的args中添加lang参数,确保查询当前语言的商品,而非默认语言版本 - 这样WooCommerce就能根据当前语言对应的货币输出正确的价格HTML
内容的提问来源于stack exchange,提问作者Romanas
相关产品推荐
相关产品推荐

