Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.9 kB
4
Indexable
Never
<?php
/**
 * Adds % tax to level 2 if billing address is in the set country.
 * 
 * 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_country_tax( $tax, $values, $order ) {
	if( !empty( $_REQUEST['level'] ) ) {
		
			$tax = round( (float) $values['price'] * 0.1, 2);
	}
	return $tax;
}

// Apply the tax if billing address is in the set.
function my_pmpro_country_tax_check() {

	if ( isset( $_REQUEST['bcountry'] ) && $_REQUEST['bcountry'] == 'AU' ) {
		add_filter( 'pmpro_tax', 'my_pmpro_country_tax', 10, 3 );
		return;
	}
	
	if ( ! function_exists( 'pmpro_start_session' ) ) {
		return;
	}
	
	// Start the session
	pmpro_start_session();
	
	if ( isset( $_SESSION['bcountry'] ) && $_SESSION['bcountry'] == 'AU' ) {
		add_filter( 'pmpro_tax', 'my_pmpro_country_tax', 10, 3 );
		return;
	}
	
}
add_action( 'pmpro_checkout_preheader_before_get_level_at_checkout', 'my_pmpro_country_tax_check' );

// Edit level cost text for non-free levels
function my_pmpro_country_tax_level_cost_text( $cost, $level ) {
	if( !pmpro_isLevelFree($level) ) {
		$country_tax = pmpro_formatPrice( round( (float) $level->initial_payment * 0.10, 2) );
		$cost .= '<br/>';
		$cost .= sprintf( '<strong>%s</strong> Tax will be added for AU customers.', esc_html( $country_tax ) );	
	}
	return $cost;	
}
add_filter( 'pmpro_level_cost_text', 'my_pmpro_country_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' );