Tổng quan về sự cố
Khi sử dụng Mailchimp for WooCommerce với WPML và WooCommerce Multilingual, bạn có thể gặp phải vấn đề sau: giỏ hàng bị bỏ quên của người mua sắm bằng ngôn ngữ phụ hiển thị mã tiền tệ mặc định của cửa hàng cùng với mức giá của người mua sắm, và liên kết khôi phục của nó mở trang thanh toán bằng ngôn ngữ mặc định.
Giải pháp tạm thời
Thêm mã này vào tệp functions.php trong giao diện của bạn. Mã này cung cấp cho Mailchimp loại tiền tệ của người mua hàng và chuyển người mua hàng mở liên kết khôi phục đến trang thanh toán bằng ngôn ngữ của họ. Mailchimp vẫn lưu trữ URL thanh toán bằng ngôn ngữ mặc định:
PHP
function wpml_mcwc_remember_cart( $email = '' ) {
if ( ! function_exists( 'WC' ) || ! WC()->session || ! class_exists( 'MailChimp_Service' ) ) {
return;
}
$session_id = (string) WC()->session->get_customer_id();
if ( $session_id ) {
set_transient( 'wpml_mcwc_currency_' . md5( $session_id ), get_woocommerce_currency(), 30 * DAY_IN_SECONDS );
}
if ( ! $email && is_user_logged_in() ) {
$email = wp_get_current_user()->user_email;
}
if ( ! $email && ! empty( $_COOKIE['mailchimp_user_email'] ) ) {
$email = sanitize_email( wp_unslash( $_COOKIE['mailchimp_user_email'] ) );
}
if ( $email ) {
set_transient( 'wpml_mcwc_lang_' . md5( trim( strtolower( $email ) ) ), apply_filters( 'wpml_current_language', null ), 30 * DAY_IN_SECONDS );
}
}
add_action( 'woocommerce_add_to_cart', function () { wpml_mcwc_remember_cart(); }, 20 );
add_action( 'woocommerce_cart_item_set_quantity', function () { wpml_mcwc_remember_cart(); }, 20 );
add_filter( 'woocommerce_update_cart_action_cart_updated', function ( $updated ) { wpml_mcwc_remember_cart(); return $updated; }, 20 );
foreach ( [ 'wp_ajax_mailchimp_set_user_by_email', 'wp_ajax_nopriv_mailchimp_set_user_by_email' ] as $wpml_mcwc_hook ) {
add_action( $wpml_mcwc_hook, function () { wpml_mcwc_remember_cart( isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '' ); }, 1 );
}
// The cart job runs in the background: give Mailchimp the shopper's currency.
add_filter( 'mailchimp_woocommerce_cart_currency_code', function ( $currency, $session_id ) {
$saved = $session_id ? get_transient( 'wpml_mcwc_currency_' . md5( (string) $session_id ) ) : false;
return $saved ? $saved : $currency;
}, 10, 2 );
// The recovery link opens the default-language checkout: send the shopper to theirs.
add_action( 'template_redirect', function () {
if ( empty( $_GET['mc_cart_id'] ) || empty( $_GET['mc_cart_token'] ) || ! is_checkout() ) {
return;
}
$cart_id = (string) wp_unslash( $_GET['mc_cart_id'] );
if ( ! preg_match( '/^[a-f0-9]{32}$/', $cart_id ) ) {
return;
}
$lang = get_transient( 'wpml_mcwc_lang_' . $cart_id );
if ( ! $lang || $lang === apply_filters( 'wpml_current_language', null ) ) {
return;
}
$page = apply_filters( 'wpml_object_id', wc_get_page_id( 'checkout' ), 'page', false, $lang );
if ( ! $page ) {
return; // no checkout in that language: stay on this one
}
$url = apply_filters( 'wpml_permalink', get_permalink( $page ), $lang, true );
wp_safe_redirect( add_query_arg( [ 'mc_cart_id' => $cart_id, 'mc_cart_token' => rawurlencode( (string) wp_unslash( $_GET['mc_cart_token'] ) ) ], $url ) );
exit;
}, 1 );