Skip to content

Instantly share code, notes, and snippets.

@scottbuscemi
Last active April 5, 2021 22:00
Show Gist options
  • Save scottbuscemi/a6ba1fbbcf9f07bb34f160fd7c9ab311 to your computer and use it in GitHub Desktop.
Save scottbuscemi/a6ba1fbbcf9f07bb34f160fd7c9ab311 to your computer and use it in GitHub Desktop.
MailChimp for WooCommerce - Automatically uncheck the newsletter signup box for EU customers using Cloudflare geoip
jQuery(document).ready(function( $ ) {
if ($('body').hasClass("eu_customer")) {
$("#mailchimp_woocommerce_newsletter").prop('checked', false);
}
});
/**
* Body class for country on checkout page
*
*/
add_filter( 'body_class','gsg_checkout_country_body_class' );
function gsg_checkout_country_body_class( $classes ) {
if (is_page('checkout')) {
$eu_countries = array("BE", "BG", "CZ", "DK", "DE", "EE", "IE", "EL", "ES", "FR", "HR", "IT", "CY", "LV", "LT", "LU", "HU", "MT", "NL", "AT", "PL", "PT", "RO", "SI", "SK", "FI", "SE");
$user_country = $_SERVER["HTTP_CF_IPCOUNTRY"];
if (in_array($user_country, $eu_countries)) {
$classes[] = "eu_customer";
}
}
return $classes;
}
function gsg_checkout_geo_script() {
if (is_page('checkout')) {
wp_enqueue_script( 'checkout-geo', plugin_dir_url( __FILE__ ) . 'js/checkout-geo.js', array ( 'jquery' ), 1.1);
}
}
add_action( 'wp_enqueue_scripts', 'gsg_checkout_geo_script' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment