Last active
July 31, 2023 17:38
-
-
Save greathmaster/e17e785fbd9453c82877fbd66f28a596 to your computer and use it in GitHub Desktop.
Set PMPro to charge in different currencies based on IP Geolocation.
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
/* | |
Set PMPro to charge in different currencies based on IP Geolocation. | |
Requires the GeoIP Detect plugin (https://wordpress.org/plugins/geoip-detect/) | |
*/ | |
function pmpro_determine_country_from_ip() | |
{ | |
global $country_from_ip; | |
//check if the GEO IP Detect plugin is active | |
if(!defined('GEOIP_DETECT_VERSION')) | |
return false; | |
if(!isset($country_from_ip)) | |
{ | |
//get the country | |
$record = geoip_detect2_get_info_from_current_ip(); | |
$country_from_ip = $record->country->isoCode; | |
if(empty($country_from_ip)) | |
$country_from_ip = false; | |
} | |
return $country_from_ip; | |
} | |
function pmpro_change_currency() | |
{ | |
global $post, $pmpro_pages, $pmpro_currency, $euro_countries; | |
$country_from_ip = pmpro_determine_country_from_ip(); | |
//for testing | |
//$country_from_ip = 'US'; | |
//if($country_from_ip == 'US') | |
// $pmpro_currency = 'USD'; | |
if(in_array($country_from_ip, $euro_countries)) | |
$pmpro_currency = 'EUR'; | |
if($country_from_ip == 'UK') | |
$pmpro_currency = 'GBP'; | |
} | |
function my_pmpro_checkout_level($level) | |
{ | |
pmpro_change_currency(); | |
return $level; | |
} | |
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level"); | |
function my_pmpro_format_price($formatted, $price, $pmpro_currency, $pmpro_currency_symbol) | |
{ | |
global $euro_countries; | |
$country_from_ip = pmpro_determine_country_from_ip(); | |
//for testing | |
//$country_from_ip = 'US'; | |
// if($country_from_ip == 'US') | |
// $formatted = '$'.$price; | |
if(in_array($country_from_ip, $euro_countries)) | |
$formatted = '€'.$price; | |
if($country_from_ip == 'UK') | |
$formatted = '£'.$price; | |
return $formatted; | |
} | |
add_filter('pmpro_format_price', 'my_pmpro_format_price', 10, 4); | |
function my_init() | |
{ | |
global $euro_countries; | |
$euro_countries = array('ES', 'SI', 'SK', 'PT', 'MT', 'LU', 'LT', 'LV', 'IT', 'IE', 'EL', 'DE', 'FR', 'FI', 'EE', 'CY', 'BE', 'AT', 'NL'); | |
} | |
add_action('init', 'my_init'); |
@itsnathanhere. Thank you for pointing this out. If I recall correctly, this did work on older versions, but I haven't tested this snippet with the latest version of PMPro. With that in mind I suggest using this as a starting point to implementing a geo location pricing display, but not as a "production ready" setup without additional testing. Also, I suggest getting in touch with the PMPro support team who should be able to offer guidance on how this could be extended in specific use cases. Cheers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's worth noting this code only changes the currency symbol displayed at checkout and not the actual price or currency itself - Stripe will still charge in the default currency and this can lead to a whole host of legal problems. Users will be charged stripe's conversion rate rather than the price advertised on your site.