Select default country based on ip [solution included]
-
Hi,
I’m managing VAT in B2B for EU customers but most of my customers are from the US. I wanted to make tax calculation made on the ip.
The problem was the default country was not selected and as a consequence, the VAT for EU customers were not showed before making the payment.Here is my contribution. Not perfect, but it works.
This plugin use goeip.inc available here :
https://github.com/maxmind/geoip-api-php/tree/master/srcand the ip database available here :
https://dev.maxmind.com/geoip/legacy/geolite/
(GeoLite Country)These 2 files must be dropped in the repository of this plugin.
<?php /** * Plugin Name: Select default country based on ip for wp ecommerce * Plugin URI: * Description: Select default country based on ip for wp ecommerce * Version: 1.0 * Author: Superman * Author URI: * License: */ function select_default_country_register() { add_action('wpsc_add_item', 'select_default_country_on_add_item', 10, 3 ); } add_action( 'init', 'select_default_country_register' ); // set the country to the one defined by the ip address // doesn't work if the user is coming from ipv6 function select_default_country_on_add_item($product_id, $parameters, $wpsc_cart) { $code = wpsc_get_customer_meta('billing_country'); if ($code === NULL || $code == "") { include("geoip.inc"); $gi = geoip_open(realpath(dirname(__FILE__)) . "/GeoIP.dat",GEOIP_STANDARD); $code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); geoip_close($gi); if (WPSC_Countries::get_country_id($code) != false && $code != "") { wpsc_update_customer_meta('shipping_country', $code); wpsc_update_customer_meta('billing_country', $code); } } }
- The topic ‘Select default country based on ip [solution included]’ is closed to new replies.