-
-
Save kimwhite/49c00e2859793d47d2040d8d277bd326 to your computer and use it in GitHub Desktop.
Apply Canadian taxes to Checkouts with PMPro - DEFAULT CHECKED
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 | |
/* | |
* Plugin Name: Paid Memberships Pro - Canadian Tax | |
* Description: Apply Canadian taxes to Checkouts with PMPro | |
* Version: .1 | |
* Author: Stranger Studios | |
* Author URI: http://www.strangerstudios.com | |
*/ | |
/* | |
* Tax solution for Canada - updated to default to checked | |
* Tax rate charged is based on the user's state. | |
* | |
* This solution is based off of tax rates in January 2020 | |
* More info: https://canadabusiness.ca/government/taxes-gst-hst/federal-tax-information/overview-of-charging-and-collecting-sales-tax/ | |
* | |
* Edit as needed, then save this file in your plugins folder and activate it through the plugins page in the WP dashboard. | |
*/ | |
/* | |
/** | |
* If Canadian resident, set tax based on selected state. | |
*/ | |
function my_pmpro_canadian_tax( $tax, $values, $order ) { | |
$tax_state = ''; | |
if ( isset( $_REQUEST['canadian_tax_state'] ) ) { | |
$tax_state = $_REQUEST['canadian_tax_state']; | |
} elseif ( isset( $_SESSION['canadian_tax_state'] ) ) { | |
$tax_state = $_SESSION['canadian_tax_state']; | |
} | |
$canadian_tax_rates = array( | |
'AB' => 0.05, | |
'BC' => 0.12, | |
'MB' => 0.12, | |
'NB' => 0.15, | |
'NL' => 0.15, | |
'NS' => 0.15, | |
'NT' => 0.05, | |
'NU' => 0.05, | |
'ON' => 0.13, | |
'PE' => 0.15, | |
'QC' => 0.14975, | |
'SK' => 0.11, | |
'YT' => 0.05, | |
); | |
if ( isset( $canadian_tax_rates[ $tax_state ] ) ) { | |
$tax = round( (float) $values[ price ] * $canadian_tax_rates[ $tax_state ], 2 ); | |
} | |
return $tax; | |
} | |
/** | |
* Edit level cost text to make users aware of tax for Canadian residents. | |
*/ | |
function my_pmpro_canadian_tax_level_cost_text( $cost, $level ) { | |
$cost .= __(' Customers in Canada will be charged tax based on their state.', 'pmpro-canadian-tax'); | |
return $cost; | |
} | |
add_filter( 'pmpro_level_cost_text', 'my_pmpro_canadian_tax_level_cost_text', 10, 2 ); | |
/** | |
* Set the default country for billing to Canada. | |
*/ | |
function my_pmpro_canadian_tax_default_country( $country ) { | |
return 'CA'; | |
} | |
add_filter( 'pmpro_default_country', 'my_pmpro_canadian_tax_default_country' ); | |
/** | |
* Add checkbox and dropdown for users to declare themselves as Canadian | |
* residents and choose their state. | |
*/ | |
function my_pmpro_canadian_tax_checkout_boxes() { | |
?> | |
<table id="pmpro_pricing_fields" class="pmpro_checkout" width="100%" cellpadding="0" cellspacing="0" border="0"> | |
<thead> | |
<tr> | |
<th> | |
<?php esc_html_e( 'Canadian Residents', 'pmpro-canadian-tax' ); ?> | |
</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td> | |
<div> | |
<input checked id="user_in_canadian_taxregion" name="user_in_canadian_taxregion" type="checkbox" value="1" onchange="my_pmpro_canadian_taxes_checkbox_changed()" <?php if ( ! empty( $_REQUEST['user_in_canadian_taxregion'] ) || ! empty( $_SESSION['user_in_canadian_taxregion'] ) ) { ?>checked="checked"<?php } ?> /> <label for="user_in_canadian_taxregion" class="pmpro_normal pmpro_clickable"><?php _e('Check this box if your billing address is in Canada.', 'pmpro-canadian-tax');?></label> | |
</div> | |
</td> | |
</tr> | |
<tr id="my_pmpro_canadian_tax_state_select_tr" <?php if( empty( $_REQUEST['user_in_canadian_taxregion'] ) && empty( $_SESSION['user_in_canadian_taxregion'] ) ) {?>show<?php } ?> > | |
<td> | |
<div> | |
<label for="canadian_tax_state"><?php esc_html_e( 'Select your province.', 'pmpro-canadian-tax' ); ?></label> | |
<select id="canadian_tax_state" name="canadian_tax_state"> | |
<?php | |
$canadian_states = array( | |
'AB' => 'Alberta', | |
'BC' => 'British Columbia', | |
'MB' => 'Manitoba', | |
'NB' => 'New Brunswick', | |
'NL' => 'Newfoundland & Labrador', | |
'NS' => 'Nova Scotia', | |
'NT' => 'Northwest Territories', | |
'NU' => 'Nunavut', | |
'ON' => 'Ontario', | |
'PE' => 'Prince Edward Island', | |
'QC' => 'Quebec', | |
'SK' => 'Saskatchewan', | |
'YT' => 'Yukon', | |
); | |
foreach ( $canadian_states as $state_code => $state_name ) { | |
$selected = ''; | |
if ( | |
( isset( $_REQUEST['canadian_tax_state'] ) && $_REQUEST['canadian_tax_state'] === $state_code ) || | |
( isset( $_SESSION['canadian_tax_state'] ) && $_SESSION['canadian_tax_state'] === $state_code ) | |
) { | |
$selected = ' selected'; | |
} | |
echo '<option value="' . $state_code . '"' . $selected . '>' . $state_name . '</option>'; | |
} | |
?> | |
</select> | |
</div> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<script> | |
function my_pmpro_canadian_taxes_checkbox_changed() | |
{ | |
if(jQuery('#user_in_canadian_taxregion').is(":checked")) | |
jQuery("#my_pmpro_canadian_tax_state_select_tr").show(); | |
else | |
jQuery("#my_pmpro_canadian_tax_state_select_tr").hide(); | |
} | |
</script> | |
<?php | |
} | |
add_action( 'pmpro_checkout_boxes', 'my_pmpro_canadian_tax_checkout_boxes' ); | |
/** | |
* Check whether user declared themself as Canadian resident. | |
*/ | |
function my_pmpro_canadian_tax_check() { | |
// Check request and session. | |
if ( isset( $_REQUEST['user_in_canadian_taxregion'] ) ) { | |
// Not empty? setup the tax function. | |
if ( ! empty( $_REQUEST['user_in_canadian_taxregion'] ) ) { | |
add_filter( 'pmpro_tax', 'my_pmpro_canadian_tax', 10, 3 ); | |
} | |
} elseif ( ! empty( $_SESSION['user_in_canadian_taxregion'] ) ) { | |
// Add the filter. | |
add_filter( 'pmpro_tax', 'my_pmpro_canadian_tax', 10, 3 ); | |
} | |
} | |
add_action( 'init', 'my_pmpro_canadian_tax_check', 22 ); | |
/** | |
* Save Canadian residence info into $_SESSION for off-site checkouts | |
*/ | |
function my_pmpro_canadian_tax_session_vars() { | |
if ( isset( $_REQUEST['user_in_canadian_taxregion'] ) && isset( $_REQUEST['canadian_tax_state'] ) ) { | |
$_SESSION['user_in_canadian_taxregion'] = $_REQUEST['user_in_canadian_taxregion']; | |
$_SESSION['canadian_tax_state'] = $_REQUEST['canadian_tax_state']; | |
} | |
} | |
add_action( 'pmpro_paypalexpress_session_vars', 'my_pmpro_canadian_tax_session_vars' ); | |
add_action( 'pmpro_before_send_to_twocheckout', 'my_pmpro_canadian_tax_session_vars', 10, 0 ); | |
/** | |
* Remove the session variables after checkout is complete. | |
*/ | |
function my_pmpro_canadian_tax_after_checkout() { | |
if ( isset( $_SESSION['user_in_canadian_taxregion'] ) ) { | |
unset( $_SESSION['user_in_canadian_taxregion'] ); | |
} | |
if ( isset( $_SESSION['canadian_tax_state'] ) ) { | |
unset( $_SESSION['canadian_tax_state'] ); | |
} | |
} | |
add_action( 'pmpro_after_checkout', 'my_pmpro_canadian_tax_after_checkout' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment