/** * Returns the Currency used in the Country to which a specific IP Address * belongs. * * @param string host A host name or IP Address. * @param string default_currency The Currency to use as a default in case the * Country currency could not be detected. * @return string|bool A currency code, or False if an error occurred. */ public function get_currency_by_host($host, $default_currency) { $ip2location = WC_Aelia_IP2Location::factory(); $country_code = $ip2location->get_country_code($host); if ($country_code === false) { Logger::log(sprintf(__('Could not retrieve Country Code for host "%s". Using ' . 'default currency: %s. Error messages (JSON): %s.', AELIA_CS_PLUGIN_TEXTDOMAIN), $host, $default_currency, json_encode($ip2location->get_errors()))); return $default_currency; } $country_currency = $this->get_country_currency($country_code); if (WC_Aelia_CurrencySwitcher::settings()->is_currency_enabled($country_currency)) { return $country_currency; } else { return $default_currency; } }
/** * Returns the country code for the user, detecting it using the IP Address, * if needed. * IMPORTANT: WooCommerce stores the billing country in its "customer" property, * while this method uses WooCommerce session when the billing country is selected. * This must be done because the tax display option is retrieved by WooCommerce * BEFORE the "customer" property is initialised. If we relied on such property, * very often it would be empty, and we would return the incorrect country code. * * @return string */ public function get_billing_country() { if (!empty($this->billing_country)) { return $this->billing_country; } $woocommerce = $this->wc(); $result = null; if (self::doing_ajax() && isset($_POST['action']) && $_POST['action'] === 'woocommerce_update_order_review') { // If user is on checkout page and changes the billing country, get the // country code and store it in the session check_ajax_referer('update-order-review', 'security'); if (isset($_POST[AELIA_CS_ARG_ORDER_BILLING_COUNTRY])) { $result = $_POST[AELIA_CS_ARG_ORDER_BILLING_COUNTRY]; Aelia_SessionManager::set_value(AELIA_CS_SESSION_BILLING_COUNTRY, $result); } } if (empty($result)) { if (isset($_POST[AELIA_CS_ARG_BILLING_COUNTRY])) { $result = $_POST[AELIA_CS_ARG_BILLING_COUNTRY]; Aelia_SessionManager::set_value(AELIA_CS_SESSION_BILLING_COUNTRY, $result); } } // If no billing country was posted, check if one was stored in the session if (empty($result)) { $result = Aelia_SessionManager::get_value(AELIA_CS_SESSION_BILLING_COUNTRY); } // If no valid currency could be retrieved from customer's details, detect // it using visitor's IP address if (empty($result)) { $result = WC_Aelia_IP2Location::factory()->get_visitor_country(); } // If everything fails, take WooCommerce customer country or base country if (empty($result)) { $result = isset($woocommerce->customer) ? $woocommerce->customer->get_country() : $woocommerce->countries->get_base_country(); } $this->billing_country = $result; return $result; }
public function ipgeolocation_settings_section_callback() { echo '<p class="ipgeolocation_api_settings">' . __('In this section you can enable the GeoLocation feature, which tries to ' . 'select a Currency automatically, depending on visitors\' IP Address the ' . 'first time they visit the shop. This feature uses GeoLite data created ' . 'by MaxMind, available from <a href="http://www.maxmind.com">http://www.maxmind.com</a>.', $this->_textdomain) . '</p>'; echo '<p class="ipgeolocation_api_settings">' . sprintf(__('GeoLite database in use: <code>%s</code>.', $this->_textdomain), WC_Aelia_IP2Location::geoip_db_file()) . '</p>'; }