Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimwhite/9b4930de9029ab35a56ee30fc443115e to your computer and use it in GitHub Desktop.
Save kimwhite/9b4930de9029ab35a56ee30fc443115e to your computer and use it in GitHub Desktop.
This gist works over a list of a countries and levels. It restricts the checkout to certain levels to certain countries. Either denying or Allowing
<?php // do not copy this line.
/**
* This recipe allow or deny the checkout of a level to certain countries.
* Modify $restriction_mode to use 'deny' or 'allow'. Modify $restricted_countries using level ids as keys and country codes as values.
*
* 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, $restriction_mode;
// Specify the restriction mode: 'deny' (default) blocks listed countries, 'allow' only permits listed countries.
$restriction_mode = 'allow'; // Change to 'allow' to reverse behavior.
// Specify country restrictions for all levels.
$restricted_countries = array_fill_keys(
array_keys(pmpro_getAllLevels()), // Get all level IDs dynamically
array( 'CH', 'RS', 'JP', 'CA', 'US', 'KR', 'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE' )
);
}
add_action( 'init', 'my_init' );
function my_pmpro_registration_checks( $value ) {
global $restricted_countries, $restriction_mode, $pmpro_msg, $pmpro_msgt;
$country = isset( $_REQUEST['bcountry'] ) ? sanitize_text_field( $_REQUEST['bcountry'] ) : '';
$level = pmpro_getLevelAtCheckout();
if ( empty( $level ) || empty( $country ) ) {
return $value;
}
$level_id = $level->id;
if ( array_key_exists( $level_id, $restricted_countries ) ) {
$country_list = $restricted_countries[ $level_id ];
if ( ( 'deny' === $restriction_mode && in_array( $country, $country_list, true ) ) ||
( 'allow' === $restriction_mode && ! in_array( $country, $country_list, true ) ) ) {
$pmpro_msg = ( 'deny' === $restriction_mode )
? 'Your country of residence is not permitted to register for this level.'
: 'Only specific countries are allowed to register for this level.';
$pmpro_msgt = 'pmpro_error';
return false;
}
}
return $value;
}
add_filter( 'pmpro_registration_checks', 'my_pmpro_registration_checks' );
function my_pmpro_level_expiration_text( $text, $level ) {
global $restricted_countries, $restriction_mode, $pmpro_countries;
if ( array_key_exists( $level->id, $restricted_countries ) ) {
$country_list = $restricted_countries[ $level->id ];
$mode_text = ( 'deny' === $restriction_mode )
? 'This level cannot be purchased if you reside in the following countries: '
: 'This level is only available for residents some countires. ';
$text .= ' ' . $mode_text;
// remove comments if you want to show what countries on the level page
//$text .= implode( ', ', array_map( function( $country_code ) use ( $pmpro_countries ) {
//return isset( $pmpro_countries[ $country_code ] ) ? $pmpro_countries[ $country_code ] : $country_code;
//}, $country_list ) );
}
return $text;
}
add_filter( 'pmpro_level_expiration_text', 'my_pmpro_level_expiration_text', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment