Created
October 21, 2020 14:53
-
-
Save ronalfy/403ed4f92220b36ef4cc60d242d9be92 to your computer and use it in GitHub Desktop.
PMPro - Canadian Tax Display
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 | |
/** | |
* 1. Adds 13% tax text to the level cost text. | |
* 2. Adds 13% tax to invoice if checking out as Canadian customer. | |
* 3. Adds 13% tax if Canadian to checkout and invoice. | |
* | |
* 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 band_my_pmpro_canadian_tax( $tax, $values, $order ) { | |
if ( isset( $_SESSION['user_in_canadian_taxregion'] ) ) { | |
$tax = round( (float) $values['price'] * 0.13, 2); | |
} | |
return $tax; | |
} | |
/** | |
* Edit level cost text to make users aware of tax for Canadian residents. | |
*/ | |
function band_my_pmpro_canadian_tax_level_cost_text( $cost, $level ) { | |
$canadian_tax = pmpro_formatPrice( round( (float) $level->initial_payment * 0.13, 2) ); | |
$cost .= '<br /><br />'; | |
$cost .= sprintf( 'Canadian customers will be charged <strong>%s</strong> tax.', esc_html( $canadian_tax ) ); | |
return $cost; | |
} | |
add_filter( 'pmpro_level_cost_text', 'band_my_pmpro_canadian_tax_level_cost_text', 10, 2 ); | |
/** | |
* Set the default country for billing to Canada. | |
*/ | |
function band_my_pmpro_canadian_tax_default_country( $country ) { | |
return 'CA'; | |
} | |
add_filter( 'pmpro_default_country', 'band_my_pmpro_canadian_tax_default_country' ); | |
/** | |
* Add checkbox and dropdown for users to declare themselves as Canadian | |
* residents and choose their state. | |
*/ | |
function band_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 id="user_in_canadian_taxregion" name="user_in_canadian_taxregion" type="checkbox" value="1" onchange="band_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> | |
</tbody> | |
</table> | |
<?php | |
} | |
add_action( 'pmpro_checkout_boxes', 'band_my_pmpro_canadian_tax_checkout_boxes' ); | |
/** | |
* Check whether user declared themself as Canadian resident. | |
*/ | |
function band_my_pmpro_canadian_tax_check() { | |
if ( isset( $_SESSION['user_in_canadian_taxregion'] ) && ! empty( $_SESSION['user_in_canadian_taxregion'] ) || ( isset( $_REQUEST['user_in_canadian_taxregion'] ) && ! empty( $_REQUEST['user_in_canadian_taxregion'] ) ) ) { | |
add_filter( 'pmpro_tax', 'band_my_pmpro_canadian_tax', 10, 3 ); | |
} | |
} | |
add_action( 'init', 'band_my_pmpro_canadian_tax_check', 22 ); | |
/** | |
* Save Canadian residence info into $_SESSION for off-site checkouts | |
*/ | |
function band_my_pmpro_canadian_tax_session_vars() { | |
if ( isset( $_REQUEST['user_in_canadian_taxregion'] ) ) { | |
$_SESSION['user_in_canadian_taxregion'] = $_REQUEST['user_in_canadian_taxregion']; | |
} | |
} | |
add_action( 'pmpro_paypalexpress_session_vars', 'band_my_pmpro_canadian_tax_session_vars' ); | |
add_action( 'pmpro_before_send_to_twocheckout', 'band_my_pmpro_canadian_tax_session_vars', 10, 0 ); | |
/** | |
* Remove the session variables after checkout is complete. | |
*/ | |
function band_my_pmpro_canadian_tax_after_checkout() { | |
if ( isset( $_SESSION['user_in_canadian_taxregion'] ) ) { | |
unset( $_SESSION['user_in_canadian_taxregion'] ); | |
} | |
} | |
/** | |
* Read in session vars and add in tax. | |
*/ | |
function band_my_pmpro_add_canadian_tax() { | |
if ( isset( $_REQUEST['user_in_canadian_taxregion'] ) ) { | |
$_SESSION['user_in_canadian_taxregion'] = $_REQUEST['user_in_canadian_taxregion']; | |
} | |
} | |
add_action( 'pmpro_checkout_before_processing', 'band_my_pmpro_add_canadian_tax' ); | |
add_action( 'pmpro_after_checkout', 'band_my_pmpro_canadian_tax_after_checkout' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment