Created
December 24, 2022 11:44
-
-
Save stefanmm/f0083bd32c6194ea661ce75bc7143214 to your computer and use it in GitHub Desktop.
Forbid Woo checkouts outside of a specific country (using Cloudflare)
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 | |
// First read: https://support.cloudflare.com/hc/en-us/articles/200168236-Configuring-Cloudflare-IP-Geolocation | |
function woo_validate_country( $fields, $errors ){ | |
$countryCode = "RS"; // List of codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 | |
if( isset($_SERVER["HTTP_CF_IPCOUNTRY"]) && $_SERVER["HTTP_CF_IPCOUNTRY"] != "" ){ | |
if( $_SERVER["HTTP_CF_IPCOUNTRY"] != $countryCode && !is_user_logged_in() ){ // ...but allow logged-in users to make orders | |
$errors->add( 'validation', 'Error...' ); // <-- Your error message here | |
} | |
} | |
} | |
add_action( 'woocommerce_after_checkout_validation', 'woo_validate_country', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment