WPML

Aperçu de l’anomalie

Lorsque vous utilisez Mailchimp for WooCommerce avec WPML et WooCommerce Multilingual, vous pouvez rencontrer le problème suivant : le panier abandonné d’un acheteur dans une langue secondaire comporte le code de devise par défaut de la boutique avec les prix de l’acheteur, et son lien de récupération ouvre la validation de commande dans la langue par défaut.

Solution de contournement

Ajoutez ce code au fichier functions.php de votre thème. Il fournit à Mailchimp la devise de l’acheteur, et renvoie un acheteur qui ouvre le lien de récupération vers la validation de commande dans sa langue. Mailchimp stocke toujours l’URL de validation de commande dans la langue par défaut :

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

Toutes les anomalies connues

Besoin d'aide pour ce problème ?

Faites une demande d’assistance et nous l’examinerons avec vous.