Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 05/01/2019 in all areas

  1. Hi everyone! I just wanted to share this with you because I think it would be useful to those who aren't necessarily familiar with PHP and want to display different currencies based upon the user's IP address. Big thanks to @Blesta.Store for sharing a big portion of this code already on the forum. This will pull prices from Blesta's API and allow you to use multiple currencies. After you've uploaded the API SDK, you're going to want to download this file: https://www.geoplugin.com/_media/webservices/geoplugin.class.phps Upload that file into your site's directory, then take the contents below and place it at the top of your site files (or the pages where you will be displaying the prices). <?php require_once('geoplugin.class.php'); $geoplugin = new geoPlugin(); $geoplugin->locate(); // create a variable for the country code $var_country_code = $geoplugin->countryCode; // redirect based on country code: if ($var_country_code == "AU") { $selected_currency = "AUD"; } else if ($var_country_code == "CA") { $selected_currency = "CAD"; } else if ($var_country_code == "AT") { $selected_currency = "EUR"; } else if ($var_country_code == "BE") { $selected_currency = "EUR"; } else if ($var_country_code == "BG") { $selected_currency = "EUR"; } else if ($var_country_code == "CY") { $selected_currency = "EUR"; } else if ($var_country_code == "CZ") { $selected_currency = "EUR"; } else if ($var_country_code == "DK") { $selected_currency = "EUR"; } else if ($var_country_code == "EE") { $selected_currency = "EUR"; } else if ($var_country_code == "FI") { $selected_currency = "EUR"; } else if ($var_country_code == "DE") { $selected_currency = "EUR"; } else if ($var_country_code == "GR") { $selected_currency = "EUR"; } else if ($var_country_code == "HU") { $selected_currency = "EUR"; } else if ($var_country_code == "IE") { $selected_currency = "EUR"; } else if ($var_country_code == "IT") { $selected_currency = "EUR"; } else if ($var_country_code == "LV") { $selected_currency = "EUR"; } else if ($var_country_code == "LU") { $selected_currency = "EUR"; } else if ($var_country_code == "MT") { $selected_currency = "EUR"; } else if ($var_country_code == "NL") { $selected_currency = "EUR"; } else if ($var_country_code == "PL") { $selected_currency = "EUR"; } else if ($var_country_code == "PT") { $selected_currency = "EUR"; } else if ($var_country_code == "RO") { $selected_currency = "EUR"; } else if ($var_country_code == "SK") { $selected_currency = "EUR"; } else if ($var_country_code == "SI") { $selected_currency = "EUR"; } else if ($var_country_code == "ES") { $selected_currency = "EUR"; } else if ($var_country_code == "SE") { $selected_currency = "EUR"; } else if ($var_country_code == "GB") { $selected_currency = "GBP"; } else if ($var_country_code == "IN") { $selected_currency = "INR"; } else if ($var_country_code == "MX") { $selected_currency = "MXN"; } else { $selected_currency = "USD"; } require_once "/home/username/public_html/blesta/api/blesta_api.php"; $user = "username"; $key = "key"; $url = "url"; $api = new BlestaApi($url, $user, $key); $company_id = 1; // Set acceptable currencies $valid_currencies = array("USD", "GBP", "EUR", "INR", "MXN", "AUD"); // Set default currency // Set another currency if given if (isset($_GET['currency']) && in_array(strtoupper($_GET['currency']), $valid_currencies)) $selected_currency = strtoupper($_GET['currency']); $price1 = getPackagePrice($api, 4, "month", 1); $price2 = getPackagePrice($api, 57, "year", 1); $price3 = getPackagePrice($api, 29, "month", 1); $price4 = getPackagePrice($api, 50, "month", 1); $price5 = getPackagePrice($api, 298, "year", 1); $price1_amount = formatCurrency($api, $price1->price, $price1->currency, $selected_currency, $company_id); $price2_amount = formatCurrency($api, $price2->price, $price2->currency, $selected_currency, $company_id); $price3_amount = formatCurrency($api, $price3->price, $price3->currency, $selected_currency, $company_id); $price4_amount = formatCurrency($api, $price4->price, $price4->currency, $selected_currency, $company_id); $price5_amount = formatCurrency($api, $price5->price, $price5->currency, $selected_currency, $company_id); function getPackagePrice($api, $package_id, $period, $term) { $package = $api->get("packages", "get", array('package_id' => $package_id))->response(); $package_price = null; foreach ($package->pricing as $price) { // Get monthly term if ($price->period == $period && $price->term == $term) { return $price; } } return false; } function formatCurrency($api, $amount, $from_currency, $to_currency, $company_id) { // Do the currency conversion $amount = $api->get("currencies", "convert", array('amount' => $amount, 'from_currency' => $from_currency, 'to_currency' => $to_currency, 'company_id' => $company_id))->response(); // Format the currency return $api->get("currencies", "toCurrency", array('value' => $amount, 'currency' => $to_currency, 'company_id' => $company_id))->response(); } ?> Next, you're going to want to create your API credentials in Blesta and fill them in appropriately. After you've done this, simply place the following code (changing the numbers appropriately) where you would like to display the prices. <?php echo $price1_amount; ?> Of course you can make modifications as you desire, but I wanted to share this with you in case anyone wanted to do the same.
    1 point
×
×
  • Create New...