Created
July 22, 2024 11:47
-
-
Save dwanjuki/f3495066decf91876b06a8e1344ad477 to your computer and use it in GitHub Desktop.
Specify shipping countries available per level
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 | |
/** | |
* Specify shipping countries available per level | |
* | |
* List of country codes: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes | |
* | |
* 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_pmpro_shipping_address_country_options() { | |
global $pmpro_user_fields; | |
// Bail if Shipping Address Add On isn't active | |
if( ! function_exists( 'pmproship_add_user_fields' ) ) { | |
return; | |
} | |
// Get pmpro_scountry field array key | |
$field_exists = false; | |
foreach( $pmpro_user_fields['Shipping Address'] as $scountry_key => $field ) { | |
if( $field->name == 'pmpro_scountry' ) { | |
$field_exists = true; | |
continue; | |
} | |
} | |
// Bail if pmpro_scountry field wasn't found | |
if( ! $field_exists ) { | |
return; | |
} | |
$level = pmpro_getLevelAtCheckout(); | |
switch ( $level->id ) { | |
case 1: | |
$countries = array('AF', 'AX', 'AL'); // countries to remove for level 1 | |
break; | |
case 2: | |
$countries = array('DZ', 'AS', 'AD'); // countries to remove for level 2 | |
break; | |
} | |
if( isset( $countries ) ) { | |
my_shipping_options_remove_countries( $countries, $scountry_key ); | |
} | |
} | |
add_action( 'pmpro_checkout_after_billing_fields', 'my_pmpro_shipping_address_country_options', 9 ); | |
function my_shipping_options_remove_countries( $countries, $scountry_key ) { | |
global $pmpro_user_fields; | |
foreach( $countries as $country ) { | |
unset( $pmpro_user_fields['Shipping Address'][$scountry_key]->options[$country] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment