问题概述
将YITH WooCommerce Ajax Search与 WPML 结合使用时,您可能会遇到以下问题:使用 WooCommerce Multilingual 多货币功能时,搜索结果中的价格为商店货币的金额,但显示的却是购物者的货币符号;在另一种语言下进行相同搜索后,紧接着执行“加载更多”搜索,可能会显示该语言的产品;您在插件设置中保存的搜索表单文本未出现在字符串翻译中;并且当语言作为 URL 参数时,“查看所有产品”会在默认语言下打开结果页面,并显示所有语言的产品。
临时解决方法
前往 WPML设置自定义 XML 配置,添加此代码并保存:
XML
<wpml-config><admin-texts><key name="yith_wcas_shortcodes_list"><key name="default"><key name="options"><key name="search-input"><key name="placeholder"/></key></key></key></key></admin-texts></wpml-config>对于价格和分页结果,请将此代码添加到您的主题的
functions.php 文件中:
PHP
add_filter( 'ywcas_store_search_query', '__return_false' );
add_filter( 'ywcas_search_result_data', function ( $data ) {
foreach ( (array) $data as $i => $r ) {
$r = (array) $r;
foreach ( [ 'min_price', 'max_price' ] as $k ) { if ( isset( $r[ $k ] ) ) { $r[ $k ] = (float) apply_filters( 'wcml_raw_price_amount', (float) $r[ $k ] ); } }
$data[ $i ] = $r;
}
return $data;
}, 20 );wp-content/mu-plugins/ 中创建一个包含此代码的文件(例如 wpml-ywcas-results-page.php)。它必须在 WPML 设置语言之前运行,因此在 functions.php 中不起作用:
PHP
<?php
// 1. The block's results URL: remove WPML's ?lang= from the site URL.
add_filter( 'ywcas_block_common_localize', function ( $data ) {
if ( isset( $data['siteURL'] ) ) {
$data['siteURL'] = strtok( (string) $data['siteURL'], '?' );
}
return $data;
} );
// 2. The results page: WPML gets the language code, the plugin its locale.
if ( isset( $_GET['ywcas'], $_GET['lang'] ) && false !== strpos( (string) $_GET['lang'], '_' ) ) {
global $wpdb;
$wpml_ywcas_locale = sanitize_text_field( wp_unslash( $_GET['lang'] ) );
$wpml_ywcas_code = $wpdb->get_var( $wpdb->prepare( "SELECT code FROM {$wpdb->prefix}icl_languages WHERE default_locale = %s", $wpml_ywcas_locale ) );
if ( $wpml_ywcas_code ) {
$_GET['lang'] = $_REQUEST['lang'] = $wpml_ywcas_code;
// WPML reads the language from the URL itself, not only from $_GET.
foreach ( [ 'REQUEST_URI', 'QUERY_STRING' ] as $wpml_ywcas_key ) {
if ( isset( $_SERVER[ $wpml_ywcas_key ] ) ) {
$_SERVER[ $wpml_ywcas_key ] = str_replace( 'lang=' . rawurlencode( $wpml_ywcas_locale ), 'lang=' . $wpml_ywcas_code, (string) $_SERVER[ $wpml_ywcas_key ] );
}
}
add_action( 'pre_get_posts', function () use ( $wpml_ywcas_locale ) {
$_GET['lang'] = $_REQUEST['lang'] = $wpml_ywcas_locale;
}, 9999998 );
}
}