Skip to content

Instantly share code, notes, and snippets.

@kimwhite
Forked from dwanjuki/my_pmpro_uk_tax.php
Last active September 8, 2023 16:15
Show Gist options
  • Save kimwhite/50f6d60028989423c442c6150e308e5b to your computer and use it in GitHub Desktop.
Save kimwhite/50f6d60028989423c442c6150e308e5b to your computer and use it in GitHub Desktop.
Adds % tax to all levels if billing address is in the set country
<?php
/**
* Adds % tax to level 2 if billing address is in the set country.
*
* 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_country_tax( $tax, $values, $order ) {
if( !empty( $_REQUEST['level'] ) ) {
$tax = round( (float) $values['price'] * 0.1, 2);
}
return $tax;
}
// Apply the tax if billing address is in the set.
function my_pmpro_country_tax_check() {
if ( isset( $_REQUEST['bcountry'] ) && $_REQUEST['bcountry'] == 'AU' ) {
add_filter( 'pmpro_tax', 'my_pmpro_country_tax', 10, 3 );
return;
}
if ( ! function_exists( 'pmpro_start_session' ) ) {
return;
}
// Start the session
pmpro_start_session();
if ( isset( $_SESSION['bcountry'] ) && $_SESSION['bcountry'] == 'AU' ) {
add_filter( 'pmpro_tax', 'my_pmpro_country_tax', 10, 3 );
return;
}
}
add_action( 'pmpro_checkout_preheader_before_get_level_at_checkout', 'my_pmpro_country_tax_check' );
// Edit level cost text for non-free levels
function my_pmpro_country_tax_level_cost_text( $cost, $level ) {
if( !pmpro_isLevelFree($level) ) {
$country_tax = pmpro_formatPrice( round( (float) $level->initial_payment * 0.10, 2) );
$cost .= '<br/>';
$cost .= sprintf( '<strong>%s</strong> Tax will be added for AU customers.', esc_html( $country_tax ) );
}
return $cost;
}
add_filter( 'pmpro_level_cost_text', 'my_pmpro_country_tax_level_cost_text', 10, 2 );
// hide default VAT level cost text
function mypmprovat_override_default_level_cost_text(){
remove_filter("pmpro_level_cost_text", "pmprovat_pmpro_level_cost_text", 10, 2);
}
add_action( 'init', 'mypmprovat_override_default_level_cost_text' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment