Guillaume Posted March 17, 2021 Share Posted March 17, 2021 Hi, I've developped a plug-in for Systempay as a payment method. Nothing too fancy here, the form they expect is fairly standard (currency code, amount, etc.) However, I want to offer the user the possibility to change the currency (if the website of course allows it). I try to retrieve the exchange rate from Cubecart's admin with: $exchange_rate = tpl_vars['CURRENT_CURRENCY'] (see gateway.class.php line 97) But that value is always empty. I believe I am calling the variable in a wrong manner. But could anyone guide me on how to retrieve it? Cheers, Guillaume SystempayCurrencyIssue.zip Link to comment Share on other sites More sharing options...
bsmither Posted March 17, 2021 Share Posted March 17, 2021 Please try: General: $myVar = $smarty->getTemplateVars('foo'); Specific: $exchange_rate = $GLOBALS['smarty']->getTemplateVars('CURRENT_CURRENCY'); However, be cautious because CubeCart might not assign a value to CURRENT_CURRENCY until after the gateway code is executed (class is instantiated or method called). Link to comment Share on other sites More sharing options...
Guillaume Posted March 18, 2021 Author Share Posted March 18, 2021 Hi, Cheers for the advice. Unfortunately the first line prevents my soft from executing, while on the second one, the variable $exchange_rate ends up being empty. Is there any thing I shall do beforehand (e.g. any import I shall do)? In regards to your last remark, I actually intend to use this piece of code in the gateway code itself. So I'm not sure if that's wise. I'm guessing that in the cart attributes, we should have both the default currency and the currency the user chose, no? Because that's displayed to the user even before we execute any payment call? Thanks in advance, Guillaume Link to comment Share on other sites More sharing options...
bsmither Posted March 18, 2021 Share Posted March 18, 2021 The first line of code is from the Smarty documentation. The Smarty docs use the established variable $smarty to hold the Smarty class instantiation in its examples. CubeCart uses $GLOBAL['smarty'] instead. The gateway class is given the session's user details, the Cart's basket, and not much else. To see this, in admin, Store Settings, Advanced tab, enable Cubecart's debug mode, followed by your local public IP address in the next field (www.showmyip.com). Now, at the bottom of every page, there is a grey section of debug info. You will see the SESSION, __client, currency. It will also include the __basket details. CubeCart manages everything using the currency that is set as the default in Store Settings. For every place that the visitor's chosen currency is to be displayed, CubeCart calls $GLOBALS['tax']->priceFormat() which calculates the exchanged value along with the necessary characters for that currency. Link to comment Share on other sites More sharing options...
Guillaume Posted March 18, 2021 Author Share Posted March 18, 2021 Hi, I actually manage to display the currency selected by the customer with: $GLOBALS['session']->get('currency', 'client') Would you know with which function I could call the corresponding exchange rate from that currency to the default one (i.e. the rate which is displayed in my Cubecart settings)? Guillaume Link to comment Share on other sites More sharing options...
Guillaume Posted March 18, 2021 Author Share Posted March 18, 2021 Enclosed are the logs I am getting in the debug mode. I can indeed see the GBP currency retrieved. However, I see no trace of the exchange rate. My assumption is that this currency characteristic (the exchange rate is not retrieved unless my piece of software especially requests this). Any hint on how I could retrieve it? Logs multi-currency.txt Link to comment Share on other sites More sharing options...
Guillaume Posted March 19, 2021 Author Share Posted March 19, 2021 Hi, I did what you mentioned, activating debug mode and checking the logs. I have tried instanciating the following: $GLOBALS['smarty']->getTemplateVars('CURRENT_CURRENCY'); tpl_vars['CURRENT_CURRENCY']->value['exchange_rate']; $CURRENT_CURRENCY[value]; //as I saw from the logs (when updating the exchange rate), that exchange rate variable goes in a field called value All of which are empty... When I try $GLOBALS['tax']->priceFormat(), my file does not execute. The good news is that I manage to retrieve the currency the user selected, in $GLOBALS['session']->get('currency', 'client'). But any hint how I could extract the exchange_rate from there? Guillaume Link to comment Share on other sites More sharing options...
bsmither Posted March 19, 2021 Share Posted March 19, 2021 I will look into this very soon. If you haven't already, please create the error log: https://forums.cubecart.com/topic/51550-how-to-create-the-error-log/ Link to comment Share on other sites More sharing options...
Guillaume Posted June 21, 2021 Author Share Posted June 21, 2021 I hqve solved this one a long time ago, so for anyone interested, here below is what I have done: #Calculation of several currency characteristics $currencyCode = $GLOBALS['tax']->_currency_vars['code']; #Numeric currency calculation $currency_records = $GLOBALS['db']->select('CubeCart_currency','iso',array('code' => $currencyCode)); if ($currency_records !== false) $numericalCurrency = $currency_records[0]['iso']; #Exchange rate calculation $currency_records = $GLOBALS['db']->select('CubeCart_currency','value',array('code' => $currencyCode)); if ($currency_records !== false) $exchange_rate = $currency_records[0]['value']; #Decimals calculation $currency_records = $GLOBALS['db']->select('CubeCart_currency','decimal_places',array('code' => $currencyCode)); if ($currency_records !== false) $decimal_places = intval($currency_records[0]['decimal_places']); Retrieving the decimal places is important as pmost payment gateways do not use the dot or comma sign and expect an amount of USD 12.45 to be sent as "1245" (hence it is important to know how many decimals the currency has). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.