Created
February 26, 2025 21:43
-
-
Save MaximilianoRicoTabo/4eafa93d65c5ca2c5033fa93c85a2f0f 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
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 // 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 = 'deny'; // Change to 'allow' to reverse behavior. | |
// Specify country restrictions per level. Array keys are level IDs, values are arrays of country codes. | |
$restricted_countries = array( | |
1 => array( 'FR', 'IT' ), | |
2 => array( 'IT' ), | |
); | |
} | |
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 of the following countries: '; | |
$text .= ' ' . $mode_text; | |
$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