Tổng quan về sự cố
Khi sử dụng YITH WooCommerce Ajax Search với WPML, bạn có thể gặp phải sự cố sau: với tính năng đa tiền tệ của WooCommerce Multilingual, giá trong kết quả tìm kiếm là số tiền theo tiền tệ của cửa hàng được hiển thị với ký hiệu tiền tệ của người mua; một tìm kiếm “load more” ngay sau cùng một tìm kiếm bằng ngôn ngữ khác có thể hiển thị sản phẩm của ngôn ngữ đó; các văn bản của biểu mẫu tìm kiếm mà bạn lưu trong cài đặt của plugin không có trong Dịch chuỗi; và với ngôn ngữ là một tham số trong URL, “See all products” mở trang kết quả bằng ngôn ngữ mặc định với sản phẩm của mọi ngôn ngữ.
Giải pháp tạm thời
Truy cập vào WPMLCài đặtCấu hình XML tùy chỉnh, thêm mã này và lưu:
<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>Đối với giá và các kết quả được phân trang, hãy thêm mã này vào tệp
functions.php trong giao diện của bạn:
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/ (ví dụ wpml-ywcas-results-page.php) với mã này. Nó phải chạy trước khi WPML thiết lập ngôn ngữ, vì vậy nó không hoạt động từ functions.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 );
}
}