WPML

问题概述

将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>
然后在 WPML字符串翻译 中翻译占位符。
对于价格和分页结果,请将此代码添加到您的主题的 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 );
如果您的网站在 URL 中使用语言作为参数(WPML设置语言 URL 格式),请在 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 );
    }
}

所有已知问题

需要有关此问题的帮助?

提出支持问题,我们将与您一起调查。