Skip to content

fix(core,starter): storefront prices follow the shopper's currency - #154

Open
moset15 wants to merge 1 commit into
emdashCommerce:mainfrom
FIKANOVA:fix/storefront-shopper-currency
Open

moset15 wants to merge 1 commit into
emdashCommerce:mainfrom
FIKANOVA:fix/storefront-shopper-currency

Conversation

@moset15

@moset15 moset15 commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

What and why

This is the bug half of #96. A shopper who switches to KES sees KES in the cart, but the home, shop, category, tag and product pages still show USD. On a first visit, before there's a cart, every listing shows each product's first listed price (often USD) while the header's currency picker already says KES. The product page's JSON-LD offer has the same problem, so search engines see a price that isn't the one on the page.

Core's ProductDetails, ProductCard and ProductGrid already take a currency prop; the starter's pages never pass it. This PR gives pages one place to learn the shopper's currency, and passes it.

The visitor-country idea in #96 isn't in this PR. It would plug in at the same point later, if you want it.

Behaviour

  • Cart responses set a dashcommerce_currency cookie (not HttpOnly, SameSite=Lax, 30 days) with the cart's currency. It's display-only: the cart stays the source of truth for pricing.
  • shopperCurrency(Astro.cookies) (new, from @dashcommerce/core/astro) returns the cart's currency, or the shop's default currency (the one a new cart starts in) before there's a cart. storefrontCurrency(Astro.cookies) reads the cookie alone; a malformed or non-ISO value counts as unset.
  • Core's components (ProductGrid, ProductDetails, the product grid and embed blocks) use shopperCurrency when no currency prop is passed; ProductCard follows the cart cookie.
  • The starter's pages (home, shop, category, tag, product) pass it to core's components and to their own price labels.
  • A product with no price in that currency shows core's existing "Not available in KES" message, or no price on a card, as it already did once a shopper had a cart.
  • Product JSON-LD uses the same currency and amount as the page. A product with no price in that currency gets no offer, rather than a price the page doesn't show.
  • Caching: pages that read the cookie vary by it, so a site that turns on page caching must cache per currency. That's in the README and the changeset.

Changes

  • packages/core/src/routes/cart.ts: a cartResponse helper sets the currency cookie alongside the session cookie (Headers, so both Set-Cookie values survive).
  • packages/core/src/astro/currency.ts (new): STOREFRONT_CURRENCY_COOKIE, storefrontCurrency, shopperCurrency; exported from @dashcommerce/core/astro.
  • packages/core/src/astro/components/*: ProductCard, ProductDetails, ProductGrid, ProductGridBlock, ProductEmbedBlock fall back to the shopper's currency.
  • packages/starter/src/lib/price.ts (new) and the five storefront pages.
  • packages/core/README.md, and a changeset (core minor, starter patch).

Tests

  • packages/core/test/storefront-currency-cookie.test.ts (new): the cookie on a new shopper's first cart response (alongside the session cookie), following a currency switch, reading it back, malformed and non-currency values, shopperCurrency's fallback to the shop's default, and that every starter listing and core's grids, blocks and product details use it.

All 9 fail without the change. Suites run locally: core 447, paystack 106, flutterwave 137, all passing. tsc --noEmit and the build are clean; Biome reports nothing new on the changed lines; bun.lock is unchanged.

Also tested by hand on a running shop (EmDash 1.6, default currency KES, products priced in KES and USD):

Check Before After
/shop, first visit (no cart) each product's first listed price, mostly USD KES on every priced product
/shop with a USD cart — USD
Product page and its JSON-LD, first visit (Enamel Mug) page KES 1,500.99; JSON-LD USD 15.99 both KES 1,500.99
JSON-LD for a product with no KES price USD offer no offer, and no price on the page
A cart response session cookie only session cookie and dashcommerce_currency=KES

Checked at 375px wide as well.

Review points

  • Why a cookie? Pages are server-rendered and need the currency before any client script runs; the cart already lives behind a session cookie, so a second cookie set on the same responses is the smallest change. Happy to shape it differently if you'd rather pages learn the currency another way.
  • Is the cookie trusted? Only for display. Prices at checkout come from the cart, so a hand-edited cookie changes what a page shows, not what anyone pays.
  • Why no JSON-LD offer when there's no price? Search engines expect the structured price to match the visible one; an offer in another currency would be a price the shopper can't see.
  • Not in this PR: the starter's announcement bar still says "Free shipping on orders over $75", which is demo copy. Separate small change.

A shopper who switched to KES saw KES in the cart but USD on the home,
shop, category, tag and product pages, and a first visit showed each
product's first listed price while the currency picker said KES.

Cart responses now set a `dashcommerce_currency` cookie, and the new
`shopperCurrency(Astro.cookies)` returns the cart's currency or, before
there is a cart, the shop's default. The starter's pages and core's
product components use it, and the product page's JSON-LD offer uses the
same currency and amount the page shows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant