Skip to content

Instantly share code, notes, and snippets.

@dwanjuki
Last active September 8, 2023 16:00
Show Gist options
  • Save dwanjuki/4a59b5b144cf6392e608c8f765542819 to your computer and use it in GitHub Desktop.
Save dwanjuki/4a59b5b144cf6392e608c8f765542819 to your computer and use it in GitHub Desktop.
Adds 20% tax to level 2 if billing address is in the UK
<?php
/**
* Adds 20% tax to level 2 if billing address is in the UK.
*
* 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_uk_tax( $tax, $values, $order ) {
if( !empty( $_REQUEST['level'] ) ) {
$level = intval( $_REQUEST['level'] );
if( $level == 2 ){
$tax = round( (float) $values['price'] * 0.2, 2);
}
}
return $tax;
}
// Apply the tax if billing address is in the UK.
function my_pmpro_uk_tax_check() {
if ( isset( $_REQUEST['bcountry'] ) && $_REQUEST['bcountry'] == 'GB' ) {
add_filter( 'pmpro_tax', 'my_pmpro_uk_tax', 10, 3 );
return;
}
if ( ! function_exists( 'pmpro_start_session' ) ) {
return;
}
// Start the session
pmpro_start_session();
if ( isset( $_SESSION['bcountry'] ) && $_SESSION['bcountry'] == 'GB' ) {
add_filter( 'pmpro_tax', 'my_pmpro_uk_tax', 10, 3 );
return;
}
}
add_action( 'pmpro_checkout_preheader_before_get_level_at_checkout', 'my_pmpro_uk_tax_check' );
// Edit level cost text for non-free levels
function my_pmpro_uk_tax_level_cost_text( $cost, $level ) {
if( !pmpro_isLevelFree($level) ) {
$uk_tax = pmpro_formatPrice( round( (float) $level->initial_payment * 0.20, 2) );
$cost .= '<br/>';
$cost .= sprintf( '<strong>%s</strong> VAT will be added for UK customers.', esc_html( $uk_tax ) );
}
return $cost;
}
add_filter( 'pmpro_level_cost_text', 'my_pmpro_uk_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