Hi there! Want
to see some
related articles?

Changing the list based on custom logic

By default the plugin subscribes the user to theMailChimp list you select on the settings screen.

However, if you have custom logic based on the user address, language, etc. that needs to change which list the customer is subscribed to you can use the `ss_wc_mailchimp_subscribe_options` hook to modify the MailChimp subscribe options:


/************ MAILCHIMP INTEGRATION HOOKS *********/

add_filter( 'ss_wc_mailchimp_subscribe_options', 'woocommerce_mailchimp_change_list', 10, 2 );
function woocommerce_mailchimp_change_list( $subscribe_options, $order_id ) { /* $subscribe_options will look like this: $subscribe_options = array( 'list_id' => '12345', 'email' => 'user@example.com', 'merge_tags' => array('FNAME' => 'Joe', 'LNAME' => 'Smith'), 'interest_groups' => array( 'abcdefg', 'hijklmn' ), // MailChimp interest group IDs 'email_type' => 'html', 'double_opt_in' => false ); */ // Change the listid (You'll need to lookup the list ID in MailChimp or by // inspecting element on the plugin settings page and making a note of // the values in the drop-down $subscribe_options['list_id'] = '12345'; // You can even change the list based on the product(s) purchased // by using the passed $order_id // Lets grab the order $order = new WC_Order( $order_id ); $order_item = $order->get_items(); foreach( $order_item as $product ) { $sku = $product->get_sku(); // here you could change the list based on a sku if ( $sku = 'abcde' ) { $subscribe_options['list_id'] = '6789'; break; } } // Optionally, change other subscribe options $subscribe_options['double_opt_in'] = true; return $subscribe_options; }

Here's an example from a user of ours who is using this to change the list based on the WPML language:


/************ MAILCHIMP INTEGRATION HOOKS *********/
add_filter( 'ss_wc_mailchimp_subscribe_options' , 'ss_wc_mailchimp_change_list_based_on_language', 10 , 1 );
function ss_wc_mailchimp_change_list_based_on_language( $subscribe_options ) { if (ICL_LANGUAGE_CODE=='en') { $subscribe_options['list_id'] = '1234'; } else { $subscribe_options['list_id'] = '5678'; } return $subscribe_options; }

 

The upcoming pro version will include an easy way to setup per-product lists and interest groups.