Developer notes
How a rate is computed
Section titled “How a rate is computed”CS-Cart stores per currency a coefficient = the value of 1 unit of that currency in the primary currency (fn_format_price_by_currency(): price in X = price in primary / coefficient of X).
Each source driver returns the publication date and, per currency, the value of 1 unit in the feed’s base currency:
| Source | URL | Base | Normalisation |
|---|---|---|---|
ecb |
https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml |
EUR | 1 / rate (the feed gives units per 1 EUR) |
nbp |
https://api.nbp.pl/api/exchangerates/tables/A/?format=json |
PLN | mid |
cnb |
https://www.cnb.cz/en/financial-markets/foreign-exchange-market/central-bank-exchange-rate-fixing/central-bank-exchange-rate-fixing/daily.txt |
CZK | rate / Amount |
tcmb |
https://www.tcmb.gov.tr/kurlar/today.xml |
TRY | (ForexBuying + ForexSelling) / 2, or one of them (setting), / Unit |
boc |
https://www.bankofcanada.ca/valet/observations/group/FX_RATES_DAILY/json?recent=1 |
CAD | value of the newest date only (recent=1 also returns discontinued series with old dates) |
Then for currency X in a store with primary currency P:
raw = value(X) / value(P) // cross rate; the source must publish X and P (or be based on one of them)final = round(raw / (1 + margin/100), decimals)A positive margin lowers the coefficient, so prices shown in X are higher by that percentage.
Guardrails, in this order: the feed answered HTTP 200 and parses; its date is not older than the “outdated” setting; the source publishes X and P; final is a finite number > 0 and <= 9999999.99999 (column double(12,5)); the change against the current coefficient is within the limit (per currency or global). A rejected value never reaches the database. A value over the limit is stored as “held” in ?:kv_cua_currencies and applied only by Accept.
Writing
Section titled “Writing”Rates are written with core fn_update_currency() (Writer::apply()), never with a bare UPDATE: the core hooks update_currency_pre / update_currency_post fire, fn_save_currencies_integrity() runs and the registry cache of ?:currencies is invalidated as for a change in Administration → Currencies. The stored currency symbol is kept byte for byte (core re-sanitises it on save). The primary currency is never written.
Product Price in Any Currency listens to update_currency_post, so its prices follow every applied rate without any code of ours.
Updater::run($trigger, $force, $user_id):
- Gate: add-on enabled and licensed (
Config::isActive()), else['ok' => false, 'reason' => 'inactive']. GET_LOCK('kv_cua_run_<hash of db name + prefix>', 0); a second run at the same time returnsreason = locked.- Currencies are grouped by source; each due source is fetched once (
Schedule::isDue(): publication time and Monday–Friday in the source’s time zone, a feed that was not yet updated is asked again after 2 hours, a failed source after 30 minutes;$force= Update now). - Per currency:
applied,unchanged(a history line once per feed date),held,failed(source error, not published, invalid value). A currency whoselast_dateequals the feed date is skipped, so repeated cron runs are no-ops. - History lines: one per event; an outage writes one line, not one per retry.
| Table | Contents |
|---|---|
cscart_kv_cua_currencies |
Per currency: source (manual, ecb, nbp, cnb, tcmb, boc), margin, max_change (-1 = global), held value (held_coef, held_raw, held_source, held_date, held_at), last_date, last_status, last_message. |
cscart_kv_cua_sources |
Per source: last check, last success, feed date, last error, fetch time, number of rates. |
cscart_kv_cua_history |
Every applied, held, approved and failed rate: currency, source, feed date, raw rate, margin, old and new coefficient, status, trigger (cron / manual), user, time, note. |
cscart_kv_cua_geo |
Geo rules: storefront_id (0 = all storefronts), country_code, currency_code (- = keep the default currency). |
cscart_kv_cua_payments |
Payment rules: payment_id, allowed currency codes (no row = all currencies). |
Uninstall keeps these tables and saves the settings in cscart_kv_keep unless Delete all data on uninstall is on.
Geo currency
Section titled “Geo currency”- Country:
CF-IPCountryonly with the Cloudflare setting on and only whenREMOTE_ADDRis a Cloudflare address or a private / listed proxy address; otherwise the GeoLite2-Country database shipped with CS-Cart (app/lib/other/maxmind/) throughMaxMind\Db\Reader(IPv4 and IPv6). The visitor address comes from the add-on’s Client IP settings (ClientIp::clientIp()). - Currency: storefront rule, then all-storefronts rule, then the ISO 4217 currency of the country; used only when that currency is active.
- Crawlers (core
CRAWLERconstant from the user agent check) never get a geo currency. - Server mode (hook
init_currency_pre): only on a storefront request withoutcurrencyparameter and without a currency in the session. A visitor’s explicit choice (?currency=XXX) is stored in the cookiekv_cua_choice(hookinit_currency_post) and restored when a later session starts. - Browser mode (automatic when
full_page_cacheorkv_full_page_cacheis active): the page carries onlywindow.kvCuaGeo = { url, current }(the same for every visitor of a page variant).js/addons/kv_currency_autopilot/func.jscallsindex.php?dispatch=kv_currency_autopilot.geoonce per browser (localStorage, no cookie of its own). The endpoint answers JSON withCache-Control: no-store, stores the currency in the session (secondary_currencyC) and, with the core Full page cache, sets itsfpc_currencycookie, which the core Varnish configuration adds to the cache hash. The script then reloads once. Our Full Page Cache keys pages by the session currency, so nothing else is needed there. A currency picked in the switcher is remembered in localStorage and sent as&set=XXXwhen a new session starts.
Test helper (development hosts only: localhost, *.local, *.test, private addresses): define('KV_CURRENCY_AUTOPILOT_TEST_IP', '194.25.2.129'); in local_conf.php makes that address the visitor address. It is ignored on any other host.
| Hook | Use |
|---|---|
init_currency_pre |
Server mode geo currency. |
init_currency_post |
Remembers an explicit choice (server mode). |
dispatch_before_display |
Storefront: browser mode data. Admin: one notice per change when rates are held or not updated. |
prepare_checkout_payment_methods |
Hides payment methods not offered in the visitor’s currency. |
controllers/frontend/checkout.pre.php refuses checkout.place_order with a payment method that is not offered in the visitor’s currency. All hooks exist with the same arguments in 4.17.2 and 4.21.2.
Endpoints
Section titled “Endpoints”- Console:
php admin.php --dispatch=kv_currency_autopilot.cron(JSON summary). - URL:
index.php?dispatch=kv_currency_autopilot.cron&cron_password=<Settings → Security → Cron password>(refused while the password is empty). index.php?dispatch=kv_currency_autopilot.geo[&set=XXX](browser mode, JSON, no-store).
Configuration overrides (config.local.php)
Section titled “Configuration overrides (config.local.php)”$config['kv_currency_autopilot_feed_urls'] = ['ecb' => 'https://mirror.example/ecb.xml']; replaces the URL of a source (a mirror or a test server). The answer must have the official format.
Network
Section titled “Network”HTTPS GET with cURL: 10 s to connect, 15 s in total, up to 2 MB, two tries on network errors and 5xx answers (1 s pause), User-Agent KavioLabs-CurrencyAutopilot/<version> (+https://cs-cart.kaviolabs.com). XML is parsed with network access off and any document with a DTD refused.
File map
Section titled “File map”app/addons/kv_currency_autopilot/ addon.xml init.php func.php controllers/backend/kv_currency_autopilot.php Rates, History (+CSV), Geo currency, Payment methods, console cron controllers/backend/kv_currency_autopilot_license.php controllers/frontend/kv_currency_autopilot.php cron URL, geo endpoint controllers/frontend/checkout.pre.php payment rule check at place order schemas/menu/menu.post.php schemas/permissions/admin.post.php schemas/permissions/vendor_multivendor.post.php schemas/permissions/vendor_ultimate.post.php Tygh/Addons/KvCurrencyAutopilot/ Config.php Currencies.php Rates.php Schedule.php Updater.php Writer.php History.php Geo.php Payments.php Hooks.php Http.php ClientIp.php Retention.php Source/ AbstractSource.php Ecb.php Nbp.php Cnb.php Tcmb.php Boc.php Sources.php SourceException.php License/ Upgrade/design/backend/templates/addons/kv_currency_autopilot/ views/kv_currency_autopilot/*.tpl, hooks, license/design/backend/css/addons/kv_currency_autopilot/styles.lessjs/addons/kv_currency_autopilot/admin.js (90-day chart, inline SVG)js/addons/kv_currency_autopilot/func.js (browser mode geo currency)var/themes_repository/responsive/templates/addons/kv_currency_autopilot/hooks/index/scripts.post.tplvar/langs/en/addons/kv_currency_autopilot.poLicense
Section titled “License”Gated (fails closed): rate updates (cron, Update now, Accept), geo currency (both modes and the endpoint) and payment rules. Not gated: admin pages (read), stored rates, history, CSV export.