Last active
April 14, 2018 07:27
-
-
Save codelance/4686883 to your computer and use it in GitHub Desktop.
Add mailchimp subscribe to woocommerce checkout.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Subscribe User to MailChimp | |
*/ | |
function wooms_mailchimp_subscribe_user($order_id,$posted){ | |
if(!empty($_POST['wooms_susbscribe']) && $_POST['wooms_susbscribe'] == '1'){ | |
try{ | |
$email = $posted['billing_email']; | |
$merge_vars = array('FNAME' => $posted['billing_first_name'],'LNAME' => $posted['billing_last_name']); | |
$api = new MCAPI(mailchimp_api_key); | |
add_post_meta( $order_id , '_customer_opted_in', 'yes'); | |
$retval = $api->listSubscribe( mailchimp_listid, $email, $merge_vars, $email_type='html', mailchimp_double_optin); | |
add_post_meta($order_id, '_mailchimp_subscribe_result', $retval ? "true" : "false"); | |
} catch (Exception $e) { add_post_meta($order_id, '_mailchimp_error', json_encode($e)); } | |
} else if( !empty($_POST['wooms_susbscribe']) && $_POST['wooms_susbscribe'] == '0' ){ | |
add_post_meta( $order_id , '_customer_opted_in', 'no'); | |
} | |
} | |
add_action('woocommerce_checkout_order_processed','wooms_mailchimp_subscribe_user',10, 2); | |
/** | |
* Display Newsletter Checkbox on Checkout | |
*/ | |
function wooms_add_newsletter_checkbox_to_checkout(){ | |
?> | |
<div class="form-row wooms"> | |
<input type="checkbox" class=".input-checkbox" name="wooms_susbscribe" id="wooms_susbscribe" value="1" checked="checked"> | |
<span class="wooms_label">Yes, I'd like to receive email updates and special offers!</span> | |
</div> | |
<?php | |
} | |
add_action('woocommerce_review_order_before_submit','wooms_add_newsletter_checkbox_to_checkout',99); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!, Thanks for this! Pasted it in the functions.php file and it works fine. But what do i have to do now? where can i call the action for putting the subscribers' email into my mailchimp list? I know it's something with the mailchimp_api_key but i don't know how to call this one. Or am i wrong? Sorry for this kind of rediculous question but i'm new to this and i try to get this working...
cheers!