Panoramica del problema
Quando utilizzi Mailchimp for WooCommerce con WPML e WooCommerce Multilingual, potresti riscontrare il seguente problema: il carrello abbandonato di un acquirente in una lingua secondaria presenta il codice della valuta predefinita del negozio con i prezzi dell’acquirente, e il suo link di recupero apre il checkout nella lingua predefinita.
Soluzione alternativa
Aggiungi questo codice al file functions.php del tuo tema. Fornisce a Mailchimp la valuta dell’acquirente e indirizza l’acquirente che apre il link di recupero al checkout nella sua lingua. Mailchimp memorizza comunque l’URL del checkout nella lingua predefinita:
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 );