Created
November 23, 2020 21:48
-
-
Save ronalfy/b5842ff0ba090fb697e46bda13872092 to your computer and use it in GitHub Desktop.
Gist UK Taxes on Checkout / Invoice / Etc.
This file contains 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 20% tax text to the level cost text. | |
* 2. Adds 20% tax to invoice if checking out as UK customer. | |
* 3. Adds 20% tax if UK 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_uk_tax( $tax, $values, $order ) { | |
if ( isset( $_SESSION['user_in_uk_taxregion'] ) ) { | |
$tax = round( (float) $values['price'] * 0.20, 2); | |
} | |
return $tax; | |
} | |
/** | |
* Edit level cost text to make users aware of tax for UK residents. | |
*/ | |
function band_my_pmpro_uk_tax_level_cost_text( $cost, $level ) { | |
$uk_tax = pmpro_formatPrice( round( (float) $level->initial_payment * 0.20, 2) ); | |
$cost .= '<br /><br />'; | |
$cost .= sprintf( 'UK customers will be charged <strong>%s</strong> tax.', esc_html( $uk_tax ) ); | |
return $cost; | |
} | |
add_filter( 'pmpro_level_cost_text', 'band_my_pmpro_uk_tax_level_cost_text', 10, 2 ); | |
/** | |
* Set the default country for billing to Canada. | |
*/ | |
function band_my_pmpro_uk_tax_default_country( $country ) { | |
return 'GB'; | |
} | |
add_filter( 'pmpro_default_country', 'band_my_pmpro_uk_tax_default_country' ); | |
/** | |
* Add checkbox and dropdown for users to declare themselves as UK | |
* residents and choose their state. | |
*/ | |
function band_my_pmpro_uk_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( 'UK Residents', 'pmpro-uk-tax' ); ?> | |
</th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<td> | |
<div> | |
<input id="user_in_uk_taxregion" name="user_in_uk_taxregion" type="checkbox" value="1" onchange="band_my_pmpro_uk_taxes_checkbox_changed()" | |
<?php | |
if ( ! empty( $_REQUEST['user_in_uk_taxregion'] ) || ! empty( $_SESSION['user_in_uk_taxregion'] ) ) { | |
?> | |
checked="checked"<?php } ?> /> <label for="user_in_uk_taxregion" class="pmpro_normal pmpro_clickable"><?php _e( 'Check this box if your billing address is in the UK.', 'pmpro-uk-tax' ); ?></label> | |
</div> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
<?php | |
} | |
add_action( 'pmpro_checkout_boxes', 'band_my_pmpro_uk_tax_checkout_boxes' ); | |
/** | |
* Check whether user declared themself as UK resident. | |
*/ | |
function band_my_pmpro_uk_tax_check() { | |
if ( isset( $_SESSION['user_in_uk_taxregion'] ) && ! empty( $_SESSION['user_in_uk_taxregion'] ) || ( isset( $_REQUEST['user_in_uk_taxregion'] ) && ! empty( $_REQUEST['user_in_uk_taxregion'] ) ) ) { | |
add_filter( 'pmpro_tax', 'band_my_pmpro_uk_tax', 10, 3 ); | |
} | |
} | |
add_action( 'init', 'band_my_pmpro_uk_tax_check', 22 ); | |
/** | |
* Save UK residence info into $_SESSION for off-site checkouts | |
*/ | |
function band_my_pmpro_uk_tax_session_vars() { | |
if ( isset( $_REQUEST['user_in_uk_taxregion'] ) ) { | |
$_SESSION['user_in_uk_taxregion'] = $_REQUEST['user_in_uk_taxregion']; | |
} | |
} | |
add_action( 'pmpro_paypalexpress_session_vars', 'band_my_pmpro_uk_tax_session_vars' ); | |
add_action( 'pmpro_before_send_to_twocheckout', 'band_my_pmpro_uk_tax_session_vars', 10, 0 ); | |
/** | |
* Remove the session variables after checkout is complete. | |
*/ | |
function band_my_pmpro_uk_tax_after_checkout() { | |
if ( isset( $_SESSION['user_in_uk_taxregion'] ) ) { | |
unset( $_SESSION['user_in_uk_taxregion'] ); | |
} | |
} | |
/** | |
* Read in session vars and add in tax. | |
*/ | |
function band_my_pmpro_add_uk_tax() { | |
if ( isset( $_REQUEST['user_in_uk_taxregion'] ) ) { | |
$_SESSION['user_in_uk_taxregion'] = $_REQUEST['user_in_uk_taxregion']; | |
} | |
} | |
add_action( 'pmpro_checkout_before_processing', 'band_my_pmpro_add_uk_tax' ); | |
add_action( 'pmpro_after_checkout', 'band_my_pmpro_uk_tax_after_checkout' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment