Hi there! Want
to see some
related articles?

Changing the list based on WPML Language

By default the WP WooCommerce Mailchimp plugin subscribes the user to the Mailchimp list you select on the settings screen.

For WPML users, you may want to customize the list based on the WPML language of the user when they completed the order. If so, you can use the ss_wc_mailchimp_subscribe_options hook to modify the Mailchimp subscribe options to set a language-specific list.

Below is an example of how to change the list based on the WPML language for the order:


/************ MAILCHIMP INTEGRATION HOOKS *********/
add_filter( 'ss_wc_mailchimp_subscribe_options' , 'ss_wc_mailchimp_change_list_based_on_language', 10 , 2 );
function ss_wc_mailchimp_change_list_based_on_language( $subscribe_options, $order_id ) {

// Use ICL_LANGUAGE_CODE line below to use the wp-admin language.
// $lang = ICL_LANGUAGE_CODE;

// The line below retrieves the language of user for the WooCommerce order.
// NOTE: In some cases the meta key below may be 'wpml_language' instead
// of '_icl_current_language'
$lang = get_post_meta( $order_id, '_icl_current_language', true );
switch ($lang) {
case 'en': $subscribe_options['list_id'] = '1234';
break;
default:
$subscribe_options['list_id'] = '5678'; }
return $subscribe_options; }

WP WooCommerce Mailchimp Pro includes an easy way to setup product-specific lists, interest groups and tags.

To achieve the same type of change of a product-specific list based on the WPML language, you use a very similar hook:


/************ WP WooCommerce Mailchimp Pro Integration Hook *********/
add_filter( 'ss_wc_mailchimp_pro_subscribe_options' , 'ss_wc_mailchimp_pro_change_list_based_on_language', 10 , 2 );

function ss_wc_mailchimp_pro_change_list_based_on_language( $subscribe_options, $order_id ) {

    // Use ICL_LANGUAGE_CODE line below to use the wp-admin language.
    // $lang = ICL_LANGUAGE_CODE;

    // The line below retrieves the language of user for the WooCommerce order.
    // NOTE: In some cases the meta key below may be 'wpml_language' instead
    // of '_icl_current_language'
    $lang = get_post_meta( $order_id, '_icl_current_language', true );

    // Assuming '1234' is a product-specific Mailchimp list id,
    // create a simple mapping for that list and WPML languages
    // Repeat for each product/language as needed
    $product_language_list_mapping = array(
        '1234' => array(
            'en' => '5678',
            'es' => '6789',
        ),
    );

    // Here, we use the passed in product-specific $subscribe_options[ 'list_id' ],
    // combined with the WPML language ($lang variable) to get the product-specific,
    // language-specific list from the $pdocut_language_list_mapping array defined above.
    $subscribe_options['list_id'] = $product_language_list_mapping[ $subscribe_options[ 'list_id' ] ][ $lang ];

    return $subscribe_options;
}