I am looking to resolve a puzzle with regards to MailChimp and WooCommerce intergration, our situation is this, we use MailChimp to send out a static coupon, they are static because of our CRM in house. Once a static coupon is sent, people can then use it on our website, however, there is nothing in place to prevent sharing this code with people who haven't signed up, so we would like to make it so when you use a particular coupon, it checks the email address you use on the site against Mailchimp, if you are on the list, the coupon applies, otherwise it does not, I was simply seeking to see if this were possible, if so how?
We're lost on this one so could use the help of the community here, we feel this thread could also become useful to others in the future, appreciate it is specific but could potentially be used in security later on.
Many Thanks in advance!
Using Drewms Mailchimp wrapper or similar you could do the following, don't forget to include your MailChimp API key and list ID.
include 'Mailchimp.php';
use \DrewM\MailChimp\MailChimp;
$MailChimp = new MailChimp('your**api***key');
function emailExistsMc($subscriberMail, $list_id){
global $MailChimp;
$subscriber_hash = $MailChimp->subscriberHash($subscriberMail);
$result = $MailChimp->get("lists/$list_id/members/$subscriber_hash");
if($result['status'] == 'subscribed') {
return true;
} else {
return false;
}
}
add_action('woocommerce_checkout_process', 'my_themes_guest_email_checker');
function my_themes_guest_email_checker() {
global $woocommerce;
$list_id = <list id here>;
// Check checkout email address against MC list, if exists proceed
if ( emailExistsMc($_POST['billing_email'], $list_id) ) {
} else {
$woocommerce->add_error( __('This coupon is not valid') );
}
}