WPML

问题概述

当将 Mailchimp for WooCommerce 与 WPML 和 WooCommerce Multilingual 结合使用时,您可能会遇到以下问题:使用次要语言的购物者的废弃购物车会显示商店的默认货币代码和购物者的价格,并且其恢复链接会以默认语言打开结账页面。

临时解决方法

将此代码添加到您主题的 functions.php 文件中。它会向 Mailchimp 提供购物者的货币,并将打开恢复链接的购物者引导至其语言的结账页面。Mailchimp 仍会存储默认语言的结账 URL:

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 );

所有已知问题

需要有关此问题的帮助?

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