Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ipokkel/52cbc67ceaec0a45ed40a136f52a7c91 to your computer and use it in GitHub Desktop.
Save ipokkel/52cbc67ceaec0a45ed40a136f52a7c91 to your computer and use it in GitHub Desktop.
Set the default VAT country based on the default PMPro country at checkout.
<?php
function my_pmpro_set_default_vat_country_on_checkout() {
// Only run on checkout page if a country isn't already set.
if ( ! function_exists( 'pmpro_is_checkout' ) || ! pmpro_is_checkout() || ! empty( $_REQUEST['eucountry'] ) || ! empty( $_SESSION['eucountry'] ) ) {
return;
}
// Get the default country and the EU countries.
$default_country = apply_filters( 'pmpro_default_country', '' );
global $pmpro_european_union;
// Get a list of the EU countries if the global is available.
if ( ! empty( $pmpro_european_union ) && is_array( $pmpro_european_union ) ) {
$vat_countries = array_keys( $pmpro_european_union );
} else {
// A fallback list in case the global is not available.
$vat_countries = array( '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', 'GB' );
}
// If the default country is a VAT country, set it for the VAT fields.
if ( in_array( $default_country, $vat_countries ) ) {
$_REQUEST['eucountry'] = $default_country;
$_SESSION['eucountry'] = $default_country;
}
}
add_action( 'pmpro_checkout_preheader', 'my_pmpro_set_default_vat_country_on_checkout' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment