Overview of the Issue
When using Mailchimp for WooCommerce with WPML and WooCommerce Multilingual, you might experience the following issue: the abandoned cart of a shopper in a secondary language has the store’s default currency code with the shopper’s prices, and its recovery link opens the checkout in the default language.
Workaround
Add this code to your theme’s functions.php file. It gives Mailchimp the shopper’s currency, and it sends a shopper who opens the recovery link to the checkout in their language. Mailchimp still stores the default-language checkout 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 );