Sending additional fields to Mailchimp
By default the plugin only sends the Mailchimp default fields of EMAIL, FNAME and LNAME. However, with a little bit of code dropped into your functions.php file you can send additional fields to Mailchimp.
Below is some sample code on how to add a custom BIRTHDAY field to the checkout screen and save it with the order info and send it along to Mailchimp:
// Make birthday a date picker mm/dd field
function ss_wc_add_checkout_field_datepicker() {
if ( is_checkout() ) {
$js = 'jQuery( "#birthday" ).css("width","125px").datepicker({ changeMonth: true, changeYear: true, yearRange:"-100:+0", dateFormat: "mm/dd" });';
// Check if WC 2.1+
if ( defined( 'WC_VERSION' ) && WC_VERSION ) {
wc_enqueue_js( $js );
} else {
global $woocommerce;
$woocommerce->add_inline_js( $js );
}
}
}
add_filter( 'wp_footer' , 'ss_wc_add_checkout_field_datepicker' );
// Save the birthday field with the order
function ss_wc_save_birthday_field( $order_id ) {
$birthday = isset( $_POST['birthday'] ) ? $_POST['birthday'] : null;
update_post_meta( $order_id, 'birthday', $birthday );
}
add_action( 'woocommerce_checkout_update_order_meta', 'ss_wc_save_birthday_field' );
// tie into the WooCommerce MailChimp 'ss_wc_mailchimp_subscribe_merge_tags' action filter to add the MailChimp merge tag for Birthday
function ss_wc_send_birthday_field_to_mailchimp( $merge_tags, $order_id, $email ) {
// retrieve the birthday from the order meta/custom fields
$birthday = get_post_meta( $order_id, 'birthday', true );
// Add 'BIRTHDAY' merge variable to MailChimp call (change 'BIRTHDAY' to whatever your merge variable is called in MailChimp)
$merge_tags['BIRTHDAY'] = $birthday;
return $merge_tags;
}
add_filter( 'ss_wc_mailchimp_subscribe_merge_tags' , 'ss_wc_send_birthday_field_to_mailchimp', 10 , 3 );