Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarrydLong/e093972edb14a24c1b796e1ebd4c837a to your computer and use it in GitHub Desktop.
Save JarrydLong/e093972edb14a24c1b796e1ebd4c837a to your computer and use it in GitHub Desktop.
Only allow for the countries in the $restricted_countries array
/**
* Only sell to specific countries by adding in the 'allowed' countries' in
* the $restricted_countries array.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_init()
{
global $restricted_countries;
//Only allow for the countries in the $restricted_countries array
$restricted_countries = array(
1 => array('FR', 'IT'),
2 => array('IT'),
);
}
add_action('init', 'my_init');
function my_pmpro_registration_checks($value)
{
global $restricted_countries, $pmpro_msg, $pmpro_msgt;
$country = $_REQUEST['bcountry'];
$level_id = $_REQUEST['level'];
//only check if the level has restrictions
if(!array_key_exists($level_id, $restricted_countries) && in_array($country, $restricted_countries[$level_id]))
{
$pmpro_msg = "Your country of residence is not permitted to register for this level.";
$pmpro_msgt = "pmpro_error";
$value = false;
}
return $value;
}
add_filter("pmpro_registration_checks", "my_pmpro_registration_checks");
function my_pmpro_level_expiration_text($text, $level)
{
global $restricted_countries, $pmpro_countries;
if(!array_key_exists($level->id, $restricted_countries ))
{
$text = $text." This level cannont be purchased if you reside in the following countries: ";
//code for commas
$i = 1;
foreach($restricted_countries[$level->id] as $country)
{
$text = $text. $pmpro_countries[$country];
if($i != count($restricted_countries[$level->id]))
$text = $text. ", ";
$i++;
}
}
return $text;
}
add_filter("pmpro_level_expiration_text", "my_pmpro_level_expiration_text", 10, 2);
@kimwhite
Copy link

I got this to work by changing the following.
line 31 should not start with ! and it should NOT be equal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment