Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/f3495066decf91876b06a8e1344ad477 to your computer and use it in GitHub Desktop.
Save dwanjuki/f3495066decf91876b06a8e1344ad477 to your computer and use it in GitHub Desktop.
Specify shipping countries available per level
<?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