Skip to content

Developer notes

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.

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):

  1. Gate: add-on enabled and licensed (Config::isActive()), else ['ok' => false, 'reason' => 'inactive'].
  2. GET_LOCK('kv_cua_run_<hash of db name + prefix>', 0); a second run at the same time returns reason = locked.
  3. 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).
  4. Per currency: applied, unchanged (a history line once per feed date), held, failed (source error, not published, invalid value). A currency whose last_date equals the feed date is skipped, so repeated cron runs are no-ops.
  5. 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.

  • Country: CF-IPCountry only with the Cloudflare setting on and only when REMOTE_ADDR is a Cloudflare address or a private / listed proxy address; otherwise the GeoLite2-Country database shipped with CS-Cart (app/lib/other/maxmind/) through MaxMind\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 CRAWLER constant from the user agent check) never get a geo currency.
  • Server mode (hook init_currency_pre): only on a storefront request without currency parameter and without a currency in the session. A visitor’s explicit choice (?currency=XXX) is stored in the cookie kv_cua_choice (hook init_currency_post) and restored when a later session starts.
  • Browser mode (automatic when full_page_cache or kv_full_page_cache is active): the page carries only window.kvCuaGeo = { url, current } (the same for every visitor of a page variant). js/addons/kv_currency_autopilot/func.js calls index.php?dispatch=kv_currency_autopilot.geo once per browser (localStorage, no cookie of its own). The endpoint answers JSON with Cache-Control: no-store, stores the currency in the session (secondary_currencyC) and, with the core Full page cache, sets its fpc_currency cookie, 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=XXX when 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.

  • 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.

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.

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.less
js/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.tpl
var/langs/en/addons/kv_currency_autopilot.po

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.