Skip to content
Cartly Developers

Markets: Country-Specific Storefronts

Run one shop, sell in many countries. Per-market currency, language, duty mode, and tax treatment — with a hard Primary Market guarantee enforced at three layers.

Overview

Markets let you define groups of countries that share a currency, language, and tax policy. Cartly routes each visitor to the matching market based on their IP country and serves market-specific prices and translated content automatically.

A Market defines:

  • countries — ISO-3166-1 alpha-2 codes
  • base_currency — display currency (e.g., "EUR")
  • default_locale / available_locales — supported languages (chip selector in admin)
  • tax_inclusive — VAT-style (tax extracted from displayed price at checkout) vs sales-tax-style (tax added on top)
  • duty_mode — "DDP" (merchant pays duties) or "DDU" (buyer pays)
  • is_primary — exactly one market is the fallback for unmatched countries

Backend module: internal/markets. Ent schemas: Market,MarketPrice, MarketProductExclusion.

Primary Market Guarantee

Every shop has exactly one primary market at all times. This invariant is enforced at three independent layers to prevent accidental storefront outages:

  1. DB partial unique index — only one row with is_primary = true per shop
  2. Ent mutation hooks — reject any write that would violate the invariant
  3. Service-layer atomic PromoteToPrimary transaction — sets new primary and clears the old one in a single DB transaction

The primary market cannot be deleted or deactivated. Its default_localedrives the platform translation source language (the language translations are authored in before being translated to other locales).

Creating a Market

bashCreate a market
curl -X POST https://cartly.pro/admin/markets \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Europe",
    "countries": ["DE", "FR", "AT", "CH", "BE", "NL"],
    "base_currency": "EUR",
    "default_locale": "de",
    "available_locales": ["de", "fr"],
    "tax_inclusive": true,
    "duty_mode": "DDP",
    "active": true
  }'

Other market endpoints:

  • GET /admin/markets — list all markets
  • GET /admin/markets/:id — market detail
  • PUT /admin/markets/:id — update settings
  • DELETE /admin/markets/:id — delete a non-primary market
  • POST /admin/markets/:id/promote — promote to primary
  • GET /admin/markets/:id/prices — list per-variant price overrides
  • PUT /admin/markets/:id/prices — bulk set price overrides
  • POST /admin/markets/:id/exclusions — exclude a product from this market

Promote to Primary

bashPromote a market to primary
// Promote a market to primary (admin confirm dialog explains translation-source impact)
curl -X POST https://cartly.pro/admin/markets/:marketId/promote \
  -H "Authorization: Bearer $ADMIN_TOKEN"
# Atomic transaction: old primary loses is_primary=true, new market gains it.
# The primary market's default_locale becomes the platform translation source language.

The admin UI shows a confirmation dialog that explains the translation-source impact. When you promote a non-English market to primary, existing translations keyed to the old source language become orphans and stop rendering until re-translated.

Locales & Language

bashUpdate market locales
// Update available locales for a market
curl -X PUT https://cartly.pro/admin/markets/:marketId \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "default_locale": "de",
    "available_locales": ["de", "fr", "en"]
  }'
// Chip selector in admin UI. Removing a locale clears the cartly_locale cookie
// for visitors who had that locale active (Max-Age=0 response header).

Endonym names (the language's name in its own script — e.g., "Deutsch", "русский", "ქართული") are sourced from internal/markets/endonym.go, which covers 19 entries with region-variant fallback. The Atrium, Balm and Bench language switchers use these automatically.

bashOrphan locale cookie handling
// When a visitor's cartly_locale cookie references an inactive market locale,
// the storefront clears it and falls back to the market default_locale.
// Response header set by the storefront context builder:
Set-Cookie: cartly_locale=; Max-Age=0; Path=/; SameSite=Lax

// Your theme does not need to handle this — it is automatic.

Tax & Duty Modes

tax_inclusive controls whether displayed prices include tax:

  • true (VAT-style) — displayed price includes tax. Checkout extracts tax from the total. Example: €29.99 incl. 20% VAT → merchant receives €24.99.
  • false (sales-tax-style) — displayed price excludes tax. Checkout adds tax on top. Example: $29.99 + 8% tax → $32.39 at checkout.

duty_mode applies to cross-border shipments:

  • DDP — Delivery Duty Paid. Merchant is responsible for import duties; the buyer sees a final all-inclusive price.
  • DDU — Delivery Duty Unpaid. Buyer pays duties upon delivery; displayed prices do not include duties.

Storefront Liquid Context

The localization object is available in every Liquid template. It is populated by internal/storefront/storefront_context.go (thebuildLocalizationContext function) and exposed as an engine global.

liquidLocalization context in Liquid
{# Liquid — localization context is available on every storefront page #}
{% assign lang = localization.language %}
{% assign market = localization.market %}

<p>You're shopping in {{ market.name }} ({{ lang.endonym_name }})</p>

{# Language switcher — endonym names come from internal/markets/endonym.go #}
<form method="post" action="/?locale={{ lang.iso_code }}">
  {% for language in localization.available_languages %}
    <button type="submit" name="locale" value="{{ language.iso_code }}">
      {{ language.endonym_name }}
    </button>
  {% endfor %}
</form>

GeoIP Routing

The Markets middleware reads the CF-IPCountry header (set by Cloudflare at the CDN edge) and matches it to the markets configured for the shop. If no market covers the visitor's country, the primary market is used as the fallback.

For locale-prefixed Storefront API routes (/storefront/v1/:locale/...), the locale parameter overrides GeoIP — useful for visitors who manually switch language/region via the storefront switcher.

The visitor's active locale is stored in the cartly_locale cookie (Secure; SameSite=Lax on HTTPS). Orphan locale values (referencing an inactive market locale) are cleared automatically.

See also: Create your first market (Help Center)