-
-
Save ashokmhrj/8ba3e31901cace9e17368a25e8ea35e0 to your computer and use it in GitHub Desktop.
Add mailchimp subscribe to woocommerce checkout.
This file contains 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
<?php | |
/** | |
* Subscribe User to MailChimp | |
*/ | |
function woo_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','woo_mailchimp_subscribe_user',10, 2); | |
/** | |
* Display Newsletter Checkbox on Checkout | |
*/ | |
function woo_add_newsletter_checkbox_to_checkout(){ | |
?> | |
<p class="form-row"> | |
<label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox"> | |
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="romani_susbscribe" id="romani_susbscribe" value="1" checked="checked"> | |
<span class="romani_label"><?php _e("Yes, I'd like to receive email updates and special offers!","romani");?></span> | |
</label> | |
</p> | |
<?php | |
} | |
add_action('woocommerce_checkout_before_terms_and_conditions','woo_add_newsletter_checkbox_to_checkout',99); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment