Last active
January 11, 2017 21:09
-
-
Save tharmann/a0e9b1ab73ad99260b83e4c3b5b24fb4 to your computer and use it in GitHub Desktop.
Add a checkbox to WooCommerce order form to process MailChimp API 3.0 list member subscriptions (placed after T&C checkbox if enabled on order form)
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 | |
//add action to process mailchimp signup if checked | |
function mc_checkout_field_process() { | |
//only run the following if the option to subscribe has been selected | |
if ( $_POST['mc-subscribe'] == 'on' ) { | |
//retrieve email, first, and last name from form | |
$email = $_POST['billing_email']; | |
$first_name = $_POST['billing_first_name']; | |
$last_name = $_POST['billing_last_name']; | |
// create a new cURL resource | |
$ch = curl_init(); | |
//unique list and API key values | |
$listid = '00-your-list-id00'; | |
$apiKey = '99-your-api-key99'; | |
//create our array of values for the subscription | |
$data_json = json_encode([ | |
'email_address' => $email, | |
'status' => 'pending', // "subscribed","unsubscribed","cleaned","pending" - pending automatically triggers double opt-in | |
'merge_fields' => [ | |
'FNAME' => $first_name, | |
'LNAME' => $last_name | |
] | |
]); | |
// set URL and other appropriate options | |
curl_setopt($ch, CURLOPT_URL, 'https://usXX.api.mailchimp.com/3.0/lists/'.$listid.'/members/'); | |
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); | |
// grab URL and pass it to the browser | |
curl_exec($ch); | |
// close cURL resource, and free up system resources | |
curl_close($ch); | |
} | |
} | |
add_action('woocommerce_checkout_process', 'mc_checkout_field_process'); | |
//save value to order record if checked | |
function mc_checkout_field_update_order_meta( $order_id ) { | |
if ( ! empty( $_POST['mc-subscribe'] ) ) { | |
update_post_meta( $order_id, 'mail-chimp-opt-in', 'Yes'); | |
} else { | |
update_post_meta( $order_id, 'mail-chimp-opt-in', 'No'); | |
} | |
} | |
add_action( 'woocommerce_checkout_update_order_meta', 'mc_checkout_field_update_order_meta' ); |
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
/*align mailchimp opt-in on checkout form*/ | |
div.form-row div.mc-subscribe { | |
clear: left; | |
} | |
div.form-row div.mc-subscribe input.mc-input-checkbox { | |
float: left!important; | |
width: auto!important; | |
margin: 5px!important; | |
} | |
div.form-row div.mc-subscribe label.mc-checkbox { | |
display: inline!important; | |
} |
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
<!--add this <div> to the existing <p class="form-row terms wc-terms-and-conditions"> element in the terms.php WC template--> | |
<div class="mc-subscribe"> | |
<input type="checkbox" class="mc-input-checkbox" name="mc-subscribe" id="mc-subscribe" /> | |
<label for="mc-subscribe" class="mc-checkbox">Sign Up for Updates and Specials</label> | |
</div> |
Great - I'm glad you could use it!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for that! Works perfect!