Last active
February 22, 2022 15:28
-
-
Save panoslyrakis/ff8de05f7f77fd063612dd80019bffef to your computer and use it in GitHub Desktop.
Set taxes per country for MarketPress. Please make sure that "mp_manage_price_tax/tax_rate" filter has been added in mp_manage_price_tax() function
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 | |
/* | |
Plugin Name: Custom taxes for MarketPress | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Overrides taxes for products and cart. Can use custom tax rates per country. You can use "WPMUDEV_MP_Country_Taxes/get_countries_info" filter to deit countries and rates | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
/** | |
* Set taxes per country for MarketPress | |
* | |
* Requires | |
* 1. MarketPress | |
* 2. filter "mp_manage_price_tax/tax_rate" in mp_manage_price_tax function | |
*/ | |
if( ! class_exists( 'WPMUDEV_MP_Country_Taxes' ) ){ | |
class WPMUDEV_MP_Country_Taxes | |
{ | |
private static $_instance = null; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_MP_Country_Taxes(); | |
} | |
return self::$_instance; | |
} | |
private function __construct(){ | |
//add_filter( 'mp/taxes_tax_amount', array( $this, 'set_tax' ) , 10, 5 ); | |
add_filter( 'mp_manage_price_tax/tax_rate', array( $this, 'set_tax' ) , 10, 5 ); | |
add_filter( 'mp_tax_rate', array( $this, 'set_tax' ), 10 ); | |
} | |
//public function set_tax( $amount, $price_with_tax_pre_compound, $key, $ar, $applied_rates ){ | |
public function set_tax( $tax_rate, $tax_inclusive = null, $include_tax_to_price = null, $special_tax = null, $special_fixed_tax = null ){ | |
$country_code = $this->get_country_code(); | |
$new_tax_rate = $this->get_country_tax( $country_code ); | |
return $new_tax_rate ? $new_tax_rate : $tax_rate; | |
} | |
public function get_country_tax( $country_code, $region=null ){ | |
//TODO | |
//Check regions also | |
$countries_taxes = $this->get_countries_info(); | |
if( ! isset( $countries_taxes[ $country_code ] ) || ! is_numeric( $countries_taxes[ $country_code ] ) ){ | |
return false; | |
} | |
return $countries_taxes[ $country_code ] / 100; | |
} | |
public function get_countries_info(){ | |
//TODO | |
// Check if country code used is 3 chars and in such case filter to 2 | |
return apply_filters( 'WPMUDEV_MP_Country_Taxes/get_countries_info', array( | |
'CA' => 15, // Canada | |
'GR' => 24 // Greece | |
)); | |
/* | |
//TODO | |
return apply_filters( 'WPMUDEV_MP_Country_Taxes/get_countries_info', array( | |
'CA' => array( // Canada | |
'default' => 15, | |
'state_code_a' => 16.7, | |
'state_code_b' => 14.5, | |
), | |
'GR' => array( // Greece | |
'default' => 24 | |
) | |
)); | |
*/ | |
} | |
public function get_country_code(){ | |
$use_user_info = true; | |
if( ! is_user_logged_in() ) $use_user_info = false; | |
else{ | |
$billling_info = mp_get_user_address( 'billing' ); | |
if( ! isset( $billling_info[ 'country' ] ) || $billling_info[ 'country' ] == '' ){ | |
$use_user_info = false; | |
} | |
} | |
if( $use_user_info ) return $billling_info[ 'country' ]; | |
$WPMUDEV_RemoteAddress = new WPMUDEV_RemoteAddress; | |
$IP = $WPMUDEV_RemoteAddress->getIpAddress(); | |
$location = json_decode( file_get_contents('http://freegeoip.net/json/'.$IP ), true ); | |
return $location['country_code']; | |
} | |
} | |
function WPMUDEV_MP_Country_Taxes_load(){ | |
$GLOBALS['WPMUDEV_MP_Country_Taxes'] = WPMUDEV_MP_Country_Taxes::get_instance(); | |
} | |
add_action( 'plugins_loaded', 'WPMUDEV_MP_Country_Taxes_load' ); | |
} | |
if( ! class_exists( 'WPMUDEV_RemoteAddress' ) ){ | |
class WPMUDEV_RemoteAddress | |
{ | |
/** | |
* Whether to use proxy addresses or not. | |
* | |
* As default this setting is disabled - IP address is mostly needed to increase | |
* security. HTTP_* are not reliable since can easily be spoofed. It can be enabled | |
* just for more flexibility, but if user uses proxy to connect to trusted services | |
* it's his/her own risk, only reliable field for IP address is $_SERVER['REMOTE_ADDR']. | |
* | |
* @var bool | |
*/ | |
protected $useProxy = false; | |
/** | |
* List of trusted proxy IP addresses | |
* | |
* @var array | |
*/ | |
protected $trustedProxies = array(); | |
/** | |
* HTTP header to introspect for proxies | |
* | |
* @var string | |
*/ | |
protected $proxyHeader = 'HTTP_X_FORWARDED_FOR'; | |
// [...] | |
/** | |
* Returns client IP address. | |
* | |
* @return string IP address. | |
*/ | |
public function getIpAddress() | |
{ | |
$ip = $this->getIpAddressFromProxy(); | |
if ($ip) { | |
return $ip; | |
} | |
// direct IP address | |
if (isset($_SERVER['REMOTE_ADDR'])) { | |
return $_SERVER['REMOTE_ADDR']; | |
} | |
return ''; | |
} | |
/** | |
* Attempt to get the IP address for a proxied client | |
* | |
* @see http://tools.ietf.org/html/draft-ietf-appsawg-http-forwarded-10#section-5.2 | |
* @return false|string | |
*/ | |
protected function getIpAddressFromProxy() | |
{ | |
if (!$this->useProxy | |
|| (isset($_SERVER['REMOTE_ADDR']) && !in_array($_SERVER['REMOTE_ADDR'], $this->trustedProxies)) | |
) { | |
return false; | |
} | |
$header = $this->proxyHeader; | |
if (!isset($_SERVER[$header]) || empty($_SERVER[$header])) { | |
return false; | |
} | |
// Extract IPs | |
$ips = explode(',', $_SERVER[$header]); | |
// trim, so we can compare against trusted proxies properly | |
$ips = array_map('trim', $ips); | |
// remove trusted proxy IPs | |
$ips = array_diff($ips, $this->trustedProxies); | |
// Any left? | |
if (empty($ips)) { | |
return false; | |
} | |
// Since we've removed any known, trusted proxy servers, the right-most | |
// address represents the first IP we do not know about -- i.e., we do | |
// not know if it is a proxy server, or a client. As such, we treat it | |
// as the originating IP. | |
// @see http://en.wikipedia.org/wiki/X-Forwarded-For | |
$ip = array_pop($ips); | |
return $ip; | |
} | |
// [...] | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment