Forked from andrewlimaza/pmpro-tax-filter-states-province.php
Last active
January 4, 2022 13:55
-
-
Save JarrydLong/1b71bc5e033ff9cbb55b2713c3fbd572 to your computer and use it in GitHub Desktop.
Custom Tax Structure For Paid Memberships Pro for States/Provinces.
This file contains hidden or 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 | |
/** | |
* This recipe will add tax based on the different Canadian regions. | |
* | |
* 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_custom_canada_tax( $tax, $values, $order ) { | |
// Get state from checkout. | |
$bstate = isset( $_REQUEST['bstate'] ) ? $_REQUEST['bstate'] : ''; | |
if ( empty( $bstate ) ) { | |
return $tax; | |
} | |
$bstate = strtoupper( $bstate ); // Make sure bstate field is lowercase. | |
$tax_val = mypmpro_return_tax_for_state( $bstate ); | |
$tax = round ( (float) $values['price'] * $tax_val, 2 ); | |
return $tax; | |
} | |
add_filter( 'pmpro_tax', 'my_pmpro_custom_canada_tax', 10, 3 ); | |
function mypmpro_return_tax_for_state( $state ){ | |
$tax_val = 0.07; // default to 7% tax. | |
switch( $state ){ | |
case 'AB': //Alberta | |
case 'BC': //British Columbia | |
case 'MB': //Manitoba | |
case 'NT': //Northwest Territories | |
case 'NU': //Nunavut | |
case 'SK': //Saskatchewan | |
case 'YT': //Yukon | |
case 'QC': //Quebec | |
$tax_val = 0.05; | |
break; | |
case 'ON': //Ontario | |
$tax_val = 0.13; | |
break; | |
case 'NB': //New Brunswick | |
case 'NL': //Newfoundland and Labrador | |
case 'NS': //Nova Scotia | |
case 'PE': //Prince Edward Island | |
$tax_val = 0.15; | |
break; | |
} | |
return $tax_val; | |
} | |
function mypmpro_tax_update_script(){ | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function(){ | |
jQuery("body").on("change", "#bstate", function(){ | |
var state = jQuery(this).val(); | |
var data = { | |
action: 'mypmpro_get_lct', | |
level: '<?php echo $_REQUEST['level']; ?>', | |
state: state | |
} | |
jQuery.post( '<?php echo admin_url('admin-ajax.php'); ?>', data, function( response ){ | |
jQuery("#pmpro_level_cost").html( response ); | |
jQuery(".pmpro_submit").prepend( response ); | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'wp_footer', 'mypmpro_tax_update_script' ); | |
function mypmpro_level_cost_text_ajax(){ | |
if( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'mypmpro_get_lct' ){ | |
global $pmpro_levels; | |
$tax_rate = mypmpro_return_tax_for_state( $_REQUEST['state'] ); | |
$level = ( isset( $_REQUEST['level'] ) ? $pmpro_levels[$_REQUEST['level']] : '' ); | |
if( !empty( $tax_rate ) ){ | |
$level->initial_payment = $level->initial_payment + ( $level->initial_payment * $tax_rate ); | |
$level->billing_amount = $level->billing_amount + ( $level->billing_amount * $tax_rate ); | |
} | |
echo pmpro_getLevelCost( $level ).' Includes 20% VAT.'; | |
wp_die(); | |
} | |
} | |
add_action( 'wp_ajax_nopriv_mypmpro_get_lct', 'mypmpro_level_cost_text_ajax' ); | |
add_action( 'wp_ajax_mypmpro_get_lct', 'mypmpro_level_cost_text_ajax' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment