Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Changed email templates to format money with the money filter (V-3738) (#14864)
Damian Legawiec614347a959b7
diff --git a/.changeset/email-money-filter.md b/.changeset/email-money-filter.mdnew file mode 100644index 0000000000..1638fbf4cf--- /dev/null+++ b/.changeset/email-money-filter.md@@ -0,0 +1,6 @@+---+"@spree/dashboard": patch+"@spree/admin-sdk": patch+---++The email template editor inserts an amount through the `money` filter (`{{ order.total | money }}`) instead of a pre-formatted `display_*` field. Email types gain the raw amounts the default templates now format: `total_minus_store_credits`, a line item's and a parcel item's `amount`, a parcel item's `price`, a fulfillment group's `cost` and `unpriced`, and a return's `refunded_total`.diff --git a/docs/developer/customization/email-variables.mdx b/docs/developer/customization/email-variables.mdxindex 4ad4931a87..d01cae3589 100644--- a/docs/developer/customization/email-variables.mdx+++ b/docs/developer/customization/email-variables.mdx@@ -38,7 +38,7 @@ These are the emails your customers receive. Every one of their templates receiv## Objects-Each object's fields, and the fields of the objects nested in it.+Each object's fields, and the fields of the objects nested in it. Format an amount with the `money` filter (`{{ order.total | money }}`); the `display_*` fields are pre-formatted copies kept for templates customized before 6.0.### `store`@@ -47,10 +47,10 @@ Each object's fields, and the fields of the objects nested in it.### `order`-- `order`: `id`, `market_id`, `withdrawal_period_ends_at`, `within_withdrawal_period`, `cart_id`, `channel_id`, `company_id`, `company_name`, `po_document_filename`, `po_document_byte_size`, `number`, `email`, `customer_note`, `po_number`, `currency`, `locale`, `total_quantity`, `coupon_code`, `fulfillment_status`, `payment_status`, `completed_at`, `item_total`, `display_item_total`, `adjustment_total`, `display_adjustment_total`, `discount_total`, `display_discount_total`, `tax_total`, `display_tax_total`, `included_tax_total`, `display_included_tax_total`, `additional_tax_total`, `display_additional_tax_total`, `total`, `display_total`, `gift_card_total`, `display_gift_card_total`, `amount_due`, `display_amount_due`, `delivery_total`, `display_delivery_total`, `fee_total`, `display_fee_total`, `store_credit_total`, `display_store_credit_total`, `covered_by_store_credit`, `customer_name`, `display_total_minus_store_credits`+- `order`: `id`, `market_id`, `withdrawal_period_ends_at`, `within_withdrawal_period`, `cart_id`, `channel_id`, `company_id`, `company_name`, `po_document_filename`, `po_document_byte_size`, `number`, `email`, `customer_note`, `po_number`, `currency`, `locale`, `total_quantity`, `coupon_code`, `fulfillment_status`, `payment_status`, `completed_at`, `item_total`, `display_item_total`, `adjustment_total`, `display_adjustment_total`, `discount_total`, `display_discount_total`, `tax_total`, `display_tax_total`, `included_tax_total`, `display_included_tax_total`, `additional_tax_total`, `display_additional_tax_total`, `total`, `display_total`, `gift_card_total`, `display_gift_card_total`, `amount_due`, `display_amount_due`, `delivery_total`, `display_delivery_total`, `fee_total`, `display_fee_total`, `store_credit_total`, `display_store_credit_total`, `covered_by_store_credit`, `customer_name`, `total_minus_store_credits`, `display_total_minus_store_credits`- `order.discounts`: `id`, `promotion_id`, `name`, `description`, `code`, `amount`, `display_amount`- `order.fees`: `id`, `label`, `kind`, `line_item_id`, `fulfillment_id`, `amount`, `display_amount`-- `order.items`: `id`, `variant_id`, `seller_id`, `preorder`, `preorder_ships_at`, `quantity`, `currency`, `name`, `slug`, `options_text`, `price`, `display_price`, `total`, `display_total`, `adjustment_total`, `display_adjustment_total`, `additional_tax_total`, `display_additional_tax_total`, `included_tax_total`, `display_included_tax_total`, `discount_total`, `display_discount_total`, `pre_tax_amount`, `display_pre_tax_amount`, `discounted_amount`, `display_discounted_amount`, `display_compare_at_amount`, `compare_at_amount`, `url`, `image_url`, `sku`, `display_amount`+- `order.items`: `id`, `variant_id`, `seller_id`, `preorder`, `preorder_ships_at`, `quantity`, `currency`, `name`, `slug`, `options_text`, `price`, `display_price`, `total`, `display_total`, `adjustment_total`, `display_adjustment_total`, `additional_tax_total`, `display_additional_tax_total`, `included_tax_total`, `display_included_tax_total`, `discount_total`, `display_discount_total`, `pre_tax_amount`, `display_pre_tax_amount`, `discounted_amount`, `display_discounted_amount`, `display_compare_at_amount`, `compare_at_amount`, `url`, `image_url`, `sku`, `amount`, `display_amount`- `order.items.option_values`: `id`, `option_type_id`, `name`, `label`, `position`, `color_code`, `option_type_name`, `option_type_label`, `image_url`- `order.fulfillments`: `id`, `number`, `tracking`, `tracking_url`, `pickup_point_data`, `selected_delivery_rate_id`, `unpriced`, `cost`, `display_cost`, `total`, `display_total`, `discount_total`, `display_discount_total`, `additional_tax_total`, `display_additional_tax_total`, `included_tax_total`, `display_included_tax_total`, `tax_total`, `display_tax_total`, `status`, `fulfillment_type`, `fulfilled_at`, `delivered_at`, `items`- `order.fulfillments.deliveries`: `id`, `tracking_number`, `carrier`, `carrier_name`, `service`, `status`, `tracking_url`, `estimated_delivery_at`, `delivered_at`@@ -70,17 +70,17 @@ Each object's fields, and the fields of the objects nested in it.### `order_group`-- `order_group`: `id`, `number`, `email`, `currency`, `total`, `display_total`, `item_total`, `display_item_total`, `fulfillment_status`, `payment_status`, `completed_at`, `customer_name`, `display_total_minus_store_credits`, `po_number`, `order_count`, `display_delivery_total`, `delivery_total`, `additional_tax_total`, `display_additional_tax_total`, `gift_card_total`, `display_gift_card_total`+- `order_group`: `id`, `number`, `email`, `currency`, `total`, `display_total`, `item_total`, `display_item_total`, `fulfillment_status`, `payment_status`, `completed_at`, `customer_name`, `total_minus_store_credits`, `display_total_minus_store_credits`, `po_number`, `order_count`, `display_delivery_total`, `delivery_total`, `additional_tax_total`, `display_additional_tax_total`, `gift_card_total`, `display_gift_card_total`- `order_group.billing_address`: `id`, `label`, `first_name`, `last_name`, `full_name`, `address1`, `address2`, `postal_code`, `city`, `phone`, `company`, `country_name`, `country_code`, `state_text`, `state_code`, `quick_checkout`, `is_default_billing`, `is_default_shipping`, `state_abbr`, `country_iso`, `state_name`- `order_group.shipping_address`: `id`, `label`, `first_name`, `last_name`, `full_name`, `address1`, `address2`, `postal_code`, `city`, `phone`, `company`, `country_name`, `country_code`, `state_text`, `state_code`, `quick_checkout`, `is_default_billing`, `is_default_shipping`, `state_abbr`, `country_iso`, `state_name`-- `order_group.items`: `id`, `variant_id`, `seller_id`, `preorder`, `preorder_ships_at`, `quantity`, `currency`, `name`, `slug`, `options_text`, `price`, `display_price`, `total`, `display_total`, `adjustment_total`, `display_adjustment_total`, `additional_tax_total`, `display_additional_tax_total`, `included_tax_total`, `display_included_tax_total`, `discount_total`, `display_discount_total`, `pre_tax_amount`, `display_pre_tax_amount`, `discounted_amount`, `display_discounted_amount`, `display_compare_at_amount`, `compare_at_amount`, `url`, `image_url`, `sku`, `display_amount`+- `order_group.items`: `id`, `variant_id`, `seller_id`, `preorder`, `preorder_ships_at`, `quantity`, `currency`, `name`, `slug`, `options_text`, `price`, `display_price`, `total`, `display_total`, `adjustment_total`, `display_adjustment_total`, `additional_tax_total`, `display_additional_tax_total`, `included_tax_total`, `display_included_tax_total`, `discount_total`, `display_discount_total`, `pre_tax_amount`, `display_pre_tax_amount`, `discounted_amount`, `display_discounted_amount`, `display_compare_at_amount`, `compare_at_amount`, `url`, `image_url`, `sku`, `amount`, `display_amount`- `order_group.items.option_values`: `id`, `option_type_id`, `name`, `label`, `position`, `color_code`, `option_type_name`, `option_type_label`, `image_url`- `order_group.promotion_discounts`: `label`, `display_amount`, `amount`- `order_group.manual_discounts`: `label`, `display_amount`, `amount`- `order_group.fee_lines`: `label`, `display_amount`, `amount`-- `order_group.fulfillment_groups`: `name`, `display_cost`, `seller_names`-- `order_group.fulfillment_groups.items`: `id`, `url`, `image_url`, `name`, `quantity`, `sku`, `options_text`, `display_price`, `display_amount`-- `order_group.unfulfilled_items`: `id`, `variant_id`, `seller_id`, `preorder`, `preorder_ships_at`, `quantity`, `currency`, `name`, `slug`, `options_text`, `price`, `display_price`, `total`, `display_total`, `adjustment_total`, `display_adjustment_total`, `additional_tax_total`, `display_additional_tax_total`, `included_tax_total`, `display_included_tax_total`, `discount_total`, `display_discount_total`, `pre_tax_amount`, `display_pre_tax_amount`, `discounted_amount`, `display_discounted_amount`, `display_compare_at_amount`, `compare_at_amount`, `url`, `image_url`, `sku`, `display_amount`+- `order_group.fulfillment_groups`: `name`, `unpriced`, `cost`, `display_cost`, `seller_names`+- `order_group.fulfillment_groups.items`: `id`, `url`, `image_url`, `name`, `quantity`, `sku`, `options_text`, `price`, `amount`, `display_price`, `display_amount`+- `order_group.unfulfilled_items`: `id`, `variant_id`, `seller_id`, `preorder`, `preorder_ships_at`, `quantity`, `currency`, `name`, `slug`, `options_text`, `price`, `display_price`, `total`, `display_total`, `adjustment_total`, `display_adjustment_total`, `additional_tax_total`, `display_additional_tax_total`, `included_tax_total`, `display_included_tax_total`, `discount_total`, `display_discount_total`, `pre_tax_amount`, `display_pre_tax_amount`, `discounted_amount`, `display_discounted_amount`, `display_compare_at_amount`, `compare_at_amount`, `url`, `image_url`, `sku`, `amount`, `display_amount`- `order_group.unfulfilled_items.option_values`: `id`, `option_type_id`, `name`, `label`, `position`, `color_code`, `option_type_name`, `option_type_label`, `image_url`- `order_group.delivery_lines`: `label`, `display_amount`, `amount`@@ -93,12 +93,12 @@ Each object's fields, and the fields of the objects nested in it.- `fulfillment.delivery_rates`: `id`, `delivery_method_id`, `name`, `selected`, `cost`, `total`, `additional_tax_total`, `included_tax_total`, `tax_total`, `carrier`, `service_level`, `estimated_delivery_date`, `unpriced`, `display_cost`, `display_total`, `display_additional_tax_total`, `display_included_tax_total`, `display_tax_total`- `fulfillment.delivery_rates.freight_summary`: `total_units`, `total_cartons`, `total_pallets`, `total_volume`, `total_weight`, `complete`- `fulfillment.delivery_rates.delivery_method`: `id`, `name`, `code`, `estimated_transit_business_days_min`, `estimated_transit_business_days_max`, `digital`, `pickup`, `pickup_point`-- `fulfillment.manifest_items`: `id`, `url`, `image_url`, `name`, `quantity`, `sku`, `options_text`, `display_price`, `display_amount`+- `fulfillment.manifest_items`: `id`, `url`, `image_url`, `name`, `quantity`, `sku`, `options_text`, `price`, `amount`, `display_price`, `display_amount`### `return`-- `return`: `id`, `number`, `status`, `order_id`, `reason_id`, `refund_total`, `display_refund_total`, `approved_at`, `received_at`, `refunded_at`, `canceled_at`, `display_refunded_total`-- `return.returned_items`: `id`, `variant_id`, `seller_id`, `preorder`, `preorder_ships_at`, `quantity`, `currency`, `name`, `slug`, `options_text`, `price`, `display_price`, `total`, `display_total`, `adjustment_total`, `display_adjustment_total`, `additional_tax_total`, `display_additional_tax_total`, `included_tax_total`, `display_included_tax_total`, `discount_total`, `display_discount_total`, `pre_tax_amount`, `display_pre_tax_amount`, `discounted_amount`, `display_discounted_amount`, `display_compare_at_amount`, `compare_at_amount`, `url`, `image_url`, `sku`, `display_amount`+- `return`: `id`, `number`, `status`, `order_id`, `reason_id`, `refund_total`, `display_refund_total`, `approved_at`, `received_at`, `refunded_at`, `canceled_at`, `refunded_total`, `display_refunded_total`+- `return.returned_items`: `id`, `variant_id`, `seller_id`, `preorder`, `preorder_ships_at`, `quantity`, `currency`, `name`, `slug`, `options_text`, `price`, `display_price`, `total`, `display_total`, `adjustment_total`, `display_adjustment_total`, `additional_tax_total`, `display_additional_tax_total`, `included_tax_total`, `display_included_tax_total`, `discount_total`, `display_discount_total`, `pre_tax_amount`, `display_pre_tax_amount`, `discounted_amount`, `display_discounted_amount`, `display_compare_at_amount`, `compare_at_amount`, `url`, `image_url`, `sku`, `amount`, `display_amount`- `return.returned_items.option_values`: `id`, `option_type_id`, `name`, `label`, `position`, `color_code`, `option_type_name`, `option_type_label`, `image_url`### `customer`diff --git a/docs/developer/customization/emails.mdx b/docs/developer/customization/emails.mdxindex 3cc64ad924..30957f431f 100644--- a/docs/developer/customization/emails.mdx+++ b/docs/developer/customization/emails.mdx@@ -78,7 +78,7 @@ Besides [Liquid's standard filters](https://shopify.github.io/liquid/filters/abs| `t` | `{{ 'order_mailer.confirm_email.dear_customer' \| t: name: order.customer_name }}` | A Spree translation, with its placeholders filled in || `raw` | `{{ product.description_html \| raw }}` | Prints the value without escaping. See below |-Most amounts already arrive formatted, as `display_total`, `display_amount` and the like, so `money` is only needed for a raw amount.+Amounts arrive as decimal strings (`total`, `amount`, `price`), so format them with `money`: `{{ order.total | money }}`. It writes the amount in the email's currency and in the customer's language, the same way for every template. The `display_*` fields (`display_total`, `display_amount`) still arrive, so templates customized before 6.0 keep working, but the default templates no longer use them and new templates should not either.## Escaping@@ -93,7 +93,7 @@ Spree builds each email's plain-text version from its HTML, writing links as `la```liquid server/app/views/spree/order_mailer/confirm_email.text.liquidThanks for your order {{ order.number }}, {{ order.customer_name }}.-Total: {{ order.display_total }}+Total: {{ order.total | money }}```Nothing is escaped in the text version.diff --git a/docs/developer/upgrades/5.6-to-6.0.mdx b/docs/developer/upgrades/5.6-to-6.0.mdxindex 68c5a2ef79..6bac0a07f7 100644--- a/docs/developer/upgrades/5.6-to-6.0.mdx+++ b/docs/developer/upgrades/5.6-to-6.0.mdx@@ -853,6 +853,7 @@ Other changes that come with it:- **Admin and Seller writes refuse JSON numbers for money.** Amounts and rates, including those inside `preferences`, must be canonical decimal strings (`"19.99"`). A number, a localized or grouped string, or more decimals than the field holds is answered with `422` and `invalid_money_format`, naming the field.- **Amounts may not carry more decimals than they can keep.** Now that the columns hold four decimals, the database no longer rounds away a fraction of a cent, so a payment, refund, store credit, gift card, fee or discount with more decimals than its currency has (`10.0049` dollars) fails validation with `too_many_decimals`. Unit prices and cost prices allow four, tax rates five decimals of the fraction (`rate_percent` `"7.125"`, not `"7.1255"`), and price list percentages three. Round in your own code before saving. A manual order discount's `value` and a commission rate's `value` are read as strictly as other money fields.- **The Admin and Seller APIs no longer return `display_*` money fields.** Responses carry the amount (`"29.99"`) with its currency, and the client formats it in the reader's own language. Before 6.0 the server formatted a copy in the request's language, which could disagree with the editable amount beside it. Format with `Intl.NumberFormat` or your platform's equivalent. The Store API, webhook payloads and email templates keep their `display_*` fields, and labels such as `display_name` are unchanged.+- **Email templates format money with the `money` filter.** The default templates print `{{ order.total | money }}` instead of `{{ order.display_total }}`; the output is the same. Email data gains the raw amounts the templates needed (`total_minus_store_credits`, a line item's `amount`, a parcel's `cost` and `unpriced`, a return's `refunded_total`). The `display_*` fields are still sent, so a template customized before 6.0 keeps working; merchants who customized one see the "updated by Spree" notice in the email editor.- **Money preferences have their own type.** Calculator, rule and store preferences holding an amount (`amount`, `first_item`, `minimal_amount`, `amount_min`, `min_amount`, `default_minimum_payout_amount` and the like) are declared `:money` instead of `:decimal`, report `money` in `preference_schema`, and are written with the decimals of the calculator's currency (`"12.50"` rather than `"12.5"`). Declare your own extensions' amounts with `:money` so the dashboard shows their currency. A `:decimal` or `:money` preference now refuses text such as `"1,599.99"` instead of reading it as 1.- **Rates are decimal strings, and tax rates are renamed.** Rates and percentages read and write as strings without trailing zeros. A tax rate's `amount` (a fraction) is now `rate`, and `amount_percentage` is `rate_percent`; `Spree::TaxRate#amount_percentage` keeps working with a warning until 6.1.- **`amount_in_cents` and `compare_at_amount_in_cents` are removed** from prices and price history. Read `amount`, and each currency's `decimal_places` (new on `GET /api/v3/store/currencies`) if you need minor units.diff --git a/docs/plans/6.0-money-correctness.md b/docs/plans/6.0-money-correctness.mdindex 8e8d6d32dd..237a5855fb 100644--- a/docs/plans/6.0-money-correctness.md+++ b/docs/plans/6.0-money-correctness.md@@ -42,6 +42,8 @@ All decided 2026-10-08; the ones marked *review* were changed or added after the- **Money preferences have their own type** (decided 2026-10-09, after spree/spree#14852). A preference is declared `:money` for an amount and `:decimal` for a rate or a measure. `:money` is stored like a decimal, reports `money` in `preference_schema`, and is written on the API with at least the decimals of the record's `currency` preference and up to four, like a unit price; with no currency it keeps its exact decimal. Why: with one `:decimal` type the server and the dashboard could only guess money from a preference's name, so calculator amounts went out as `"12.5"` while every other amount was `"12.50"`. Both types refuse text such as `"1,599.99"`, which the old lenient cast read as 1. Ships in PR D.- **The Admin API drops `display_*` fields** (decided 2026-10-09). Every amount is an exact decimal string with its currency, so the dashboard and seller panel format money themselves, in the admin's own language (`useMoneyLocale`); the server's strings were formatted in the request's language and could disagree with the editable fields beside them. The Store API keeps them for storefronts, webhooks and email templates. Ships in PR E, after the dashboards stop reading them.+- **Email templates format money with the `money` filter** (decided 2026-10-10). The default templates print raw amounts through `money` instead of the serializers' `display_*` fields, which gives the same output and leaves one way to format money in a template. The `display_*` fields stay in email data, so a template a merchant customized before 6.0 keeps rendering; whether to remove them later is a separate decision, since customized templates live in the database.+## Design Details### Inventory: every `.to_f` and `Float(` outside specs (58 sites at `4ca24b8`)diff --git a/packages/admin-sdk/src/types/generated/emails/EmailFulfillmentGroup.ts b/packages/admin-sdk/src/types/generated/emails/EmailFulfillmentGroup.tsindex f92f4f0240..9784a1d76e 100644--- a/packages/admin-sdk/src/types/generated/emails/EmailFulfillmentGroup.ts+++ b/packages/admin-sdk/src/types/generated/emails/EmailFulfillmentGroup.ts@@ -1,10 +1,12 @@-// Typelizer digest 1ac70b65123bd8028d5f98ae3c2cad09+// Typelizer digest 3489d6d3770fd6af28ae0fa2c73eba12//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {EmailParcelItem} from '.'interface EmailFulfillmentGroup {name: string | null;+ unpriced: boolean;+ cost: string;display_cost: string;seller_names: Array<string>;items: Array<EmailParcelItem>;diff --git a/packages/admin-sdk/src/types/generated/emails/EmailLineItem.ts b/packages/admin-sdk/src/types/generated/emails/EmailLineItem.tsindex 4408cc4937..a4a1225651 100644--- a/packages/admin-sdk/src/types/generated/emails/EmailLineItem.ts+++ b/packages/admin-sdk/src/types/generated/emails/EmailLineItem.ts@@ -1,4 +1,4 @@-// Typelizer digest e666cdad0d6d6d9a737e41483bdab0e8+// Typelizer digest 9fc6250e889f0ea74c3df00a42542fd6//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {EmailOptionValue} from '.'@@ -36,6 +36,7 @@ interface EmailLineItem {url: string | null;image_url: string | null;sku: string | null;+ amount: string;display_amount: string;}diff --git a/packages/admin-sdk/src/types/generated/emails/EmailOrder.ts b/packages/admin-sdk/src/types/generated/emails/EmailOrder.tsindex a4cef9f5cb..eb9d736067 100644--- a/packages/admin-sdk/src/types/generated/emails/EmailOrder.ts+++ b/packages/admin-sdk/src/types/generated/emails/EmailOrder.ts@@ -1,4 +1,4 @@-// Typelizer digest 32e6f2e8358663b8cee2798d74f4f470+// Typelizer digest 56e482a620888128fd80f86fdebafa43//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {EmailAppliedPromotion, EmailFee, EmailLineItem, EmailFulfillment, EmailPayment, EmailAddress, EmailGiftCard, EmailMarket, EmailAmountLine} from '.'@@ -60,6 +60,7 @@ interface EmailOrder {gift_card: EmailGiftCard | null;market: EmailMarket | null;customer_name: string;+ total_minus_store_credits: string;display_total_minus_store_credits: string;promotion_discounts: Array<EmailAmountLine>;manual_discounts: Array<EmailAmountLine>;diff --git a/packages/admin-sdk/src/types/generated/emails/EmailOrderGroup.ts b/packages/admin-sdk/src/types/generated/emails/EmailOrderGroup.tsindex 8f108727a2..a9a4139b59 100644--- a/packages/admin-sdk/src/types/generated/emails/EmailOrderGroup.ts+++ b/packages/admin-sdk/src/types/generated/emails/EmailOrderGroup.ts@@ -1,4 +1,4 @@-// Typelizer digest 117de1f401b172d84ac107f35c446e4b+// Typelizer digest 3f61504d7418841f9174c638518d0090//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {EmailAddress, EmailLineItem, EmailAmountLine, EmailFulfillmentGroup} from '.'@@ -18,6 +18,7 @@ interface EmailOrderGroup {billing_address: EmailAddress | null;shipping_address: EmailAddress | null;customer_name: string;+ total_minus_store_credits: string;display_total_minus_store_credits: string;items: Array<EmailLineItem>;promotion_discounts: Array<EmailAmountLine>;diff --git a/packages/admin-sdk/src/types/generated/emails/EmailParcelItem.ts b/packages/admin-sdk/src/types/generated/emails/EmailParcelItem.tsindex 32764ccbe5..8d162a5f8c 100644--- a/packages/admin-sdk/src/types/generated/emails/EmailParcelItem.ts+++ b/packages/admin-sdk/src/types/generated/emails/EmailParcelItem.ts@@ -1,4 +1,4 @@-// Typelizer digest 98cf60c18e723f902a50e17a010e7816+// Typelizer digest f2645f463e6187c2ee90f8101f8a1bcf//// DO NOT MODIFY: This file was automatically generated by Typelizer.@@ -10,6 +10,8 @@ interface EmailParcelItem {quantity: number;sku: string | null;options_text: string | null;+ price: string;+ amount: string;display_price: string;display_amount: string;}diff --git a/packages/admin-sdk/src/types/generated/emails/EmailReturn.ts b/packages/admin-sdk/src/types/generated/emails/EmailReturn.tsindex 8d7d9d5aa2..5087a0a5f0 100644--- a/packages/admin-sdk/src/types/generated/emails/EmailReturn.ts+++ b/packages/admin-sdk/src/types/generated/emails/EmailReturn.ts@@ -1,4 +1,4 @@-// Typelizer digest ba8a0cdfd53df1bee4bccce8f3ae52d6+// Typelizer digest e601a1b03e815505bd403d7939648887//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {EmailLineItem, ReturnStatus} from '.'@@ -17,6 +17,7 @@ interface EmailReturn {received_at: string | null;refunded_at: string | null;canceled_at: string | null;+ refunded_total: string;display_refunded_total: string;returned_items: Array<EmailLineItem>;}diff --git a/packages/admin-sdk/src/zod/generated/emails/EmailFulfillmentGroup.ts b/packages/admin-sdk/src/zod/generated/emails/EmailFulfillmentGroup.tsindex 6743673112..ca2c298b74 100644--- a/packages/admin-sdk/src/zod/generated/emails/EmailFulfillmentGroup.ts+++ b/packages/admin-sdk/src/zod/generated/emails/EmailFulfillmentGroup.ts@@ -4,6 +4,8 @@ import { EmailParcelItemSchema } from './EmailParcelItem';export const EmailFulfillmentGroupSchema = z.object({name: z.string().nullable(),+ unpriced: z.boolean(),+ cost: z.string(),display_cost: z.string(),seller_names: z.array(z.string()),items: z.array(EmailParcelItemSchema),diff --git a/packages/admin-sdk/src/zod/generated/emails/EmailLineItem.ts b/packages/admin-sdk/src/zod/generated/emails/EmailLineItem.tsindex 110d6be41d..9ed710f96a 100644--- a/packages/admin-sdk/src/zod/generated/emails/EmailLineItem.ts+++ b/packages/admin-sdk/src/zod/generated/emails/EmailLineItem.ts@@ -35,6 +35,7 @@ export const EmailLineItemSchema = z.object({url: z.string().nullable(),image_url: z.string().nullable(),sku: z.string().nullable(),+ amount: z.string(),display_amount: z.string(),});diff --git a/packages/admin-sdk/src/zod/generated/emails/EmailOrder.ts b/packages/admin-sdk/src/zod/generated/emails/EmailOrder.tsindex 6588cf0451..78ee79b24a 100644--- a/packages/admin-sdk/src/zod/generated/emails/EmailOrder.ts+++ b/packages/admin-sdk/src/zod/generated/emails/EmailOrder.ts@@ -67,6 +67,7 @@ export const EmailOrderSchema = z.object({gift_card: EmailGiftCardSchema.nullable(),market: EmailMarketSchema.nullable(),customer_name: z.string(),+ total_minus_store_credits: z.string(),display_total_minus_store_credits: z.string(),promotion_discounts: z.array(EmailAmountLineSchema),manual_discounts: z.array(EmailAmountLineSchema),diff --git a/packages/admin-sdk/src/zod/generated/emails/EmailOrderGroup.ts b/packages/admin-sdk/src/zod/generated/emails/EmailOrderGroup.tsindex 72bf7f1574..e84253cb42 100644--- a/packages/admin-sdk/src/zod/generated/emails/EmailOrderGroup.ts+++ b/packages/admin-sdk/src/zod/generated/emails/EmailOrderGroup.ts@@ -20,6 +20,7 @@ export const EmailOrderGroupSchema = z.object({billing_address: EmailAddressSchema.nullable(),shipping_address: EmailAddressSchema.nullable(),customer_name: z.string(),+ total_minus_store_credits: z.string(),display_total_minus_store_credits: z.string(),items: z.array(EmailLineItemSchema),promotion_discounts: z.array(EmailAmountLineSchema),diff --git a/packages/admin-sdk/src/zod/generated/emails/EmailParcelItem.ts b/packages/admin-sdk/src/zod/generated/emails/EmailParcelItem.tsindex 7f6a2dade7..d7d87cb9a8 100644--- a/packages/admin-sdk/src/zod/generated/emails/EmailParcelItem.ts+++ b/packages/admin-sdk/src/zod/generated/emails/EmailParcelItem.ts@@ -9,6 +9,8 @@ export const EmailParcelItemSchema = z.object({quantity: z.number(),sku: z.string().nullable(),options_text: z.string().nullable(),+ price: z.string(),+ amount: z.string(),display_price: z.string(),display_amount: z.string(),});diff --git a/packages/admin-sdk/src/zod/generated/emails/EmailReturn.ts b/packages/admin-sdk/src/zod/generated/emails/EmailReturn.tsindex f2c479fab7..568ca6359a 100644--- a/packages/admin-sdk/src/zod/generated/emails/EmailReturn.ts+++ b/packages/admin-sdk/src/zod/generated/emails/EmailReturn.ts@@ -16,6 +16,7 @@ export const EmailReturnSchema = z.object({received_at: z.string().nullable(),refunded_at: z.string().nullable(),canceled_at: z.string().nullable(),+ refunded_total: z.string(),display_refunded_total: z.string(),returned_items: z.array(EmailLineItemSchema),});diff --git a/packages/dashboard/src/components/spree/email-templates/variables-card.tsx b/packages/dashboard/src/components/spree/email-templates/variables-card.tsxindex ca8df071d0..1b48d2e959 100644--- a/packages/dashboard/src/components/spree/email-templates/variables-card.tsx+++ b/packages/dashboard/src/components/spree/email-templates/variables-card.tsx@@ -1,6 +1,6 @@import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@spree/dashboard-ui'import { useTranslation } from 'react-i18next'-import { templateVariables } from '../../../lib/email-template-variables'+import { insertionFor, templateVariables } from '../../../lib/email-template-variables'/** The variables a template receives, with the fields worth pointing out. */export function EmailTemplateVariablesCard({@@ -34,7 +34,7 @@ export function EmailTemplateVariablesCard({key={field}type="button"aria-label={t('admin.email_templates.variables.insert', { variable: path })}- onClick={() => onInsert(`{{ ${path} }}`)}+ onClick={() => onInsert(insertionFor(variable, field))}className="rounded bg-muted px-1.5 py-0.5 font-mono text-xs hover:bg-accent-strong focus-visible:outline-2 focus-visible:outline-ring">{path}diff --git a/packages/dashboard/src/lib/email-template-variables.test.ts b/packages/dashboard/src/lib/email-template-variables.test.tsindex c08fb8916b..b4dff4ae8e 100644--- a/packages/dashboard/src/lib/email-template-variables.test.ts+++ b/packages/dashboard/src/lib/email-template-variables.test.ts@@ -1,5 +1,10 @@import { describe, expect, it } from 'vitest'-import { documentedPaths, flattenVariables } from './email-template-variables'+import {+ documentedPaths,+ flattenVariables,+ insertionFor,+ templateVariables,+} from './email-template-variables'describe('flattenVariables', () => {it('lists dotted paths with samples, reading a list through its first item', () => {@@ -37,3 +42,15 @@ describe('documentedPaths', () => {expect(paths).not.toContain('order.billing_address.metadata')})})++describe('insertionFor', () => {+ it('inserts an amount through the money filter and anything else as it is', () => {+ const order = templateVariables('spree.order_mailer.confirm_email').find(+ (variable) => variable.name === 'order',+ )+ if (!order) throw new Error('order variable missing')++ expect(insertionFor(order, 'total')).toBe('{{ order.total | money }}')+ expect(insertionFor(order, 'number')).toBe('{{ order.number }}')+ })+})diff --git a/packages/dashboard/src/lib/email-template-variables.ts b/packages/dashboard/src/lib/email-template-variables.tsindex 0b7761bbdc..eb1711154a 100644--- a/packages/dashboard/src/lib/email-template-variables.ts+++ b/packages/dashboard/src/lib/email-template-variables.ts@@ -25,35 +25,53 @@ export interface EmailTemplateVariable {/** Translation key for one line saying what it is. */descriptionKey: stringfields?: readonly string[]+ /** The fields holding an amount, inserted through the `money` filter. */+ moneyFields?: readonly string[]}const variable = (name: string,fields?: readonly string[],schema?: unknown,+ moneyFields?: readonly string[],): EmailTemplateVariable => ({name,descriptionKey: `admin.email_templates.variables.${name}`,fields,schema,+ moneyFields,})+/**+ * The Liquid a field's chip inserts: an amount goes through the `money`+ * filter, which formats it in the email's currency and language.+ */+export function insertionFor(variable: EmailTemplateVariable, field: string): string {+ const path = `${variable.name}.${field}`+ return variable.moneyFields?.includes(field) ? `{{ ${path} | money }}` : `{{ ${path} }}`+}+const ORDER_FIELDS = ['number','customer_name','email','completed_at','items',- 'display_item_total',- 'display_delivery_total',- 'display_tax_total',- 'display_total',+ 'item_total',+ 'delivery_total',+ 'tax_total',+ 'total','shipping_address','billing_address','payments',] as const satisfies FieldsOf<EmailOrder>-const order = variable('order', ORDER_FIELDS, EmailOrderSchema)+const order = variable('order', ORDER_FIELDS, EmailOrderSchema, [+ 'item_total',+ 'delivery_total',+ 'tax_total',+ 'total',+] satisfies FieldsOf<EmailOrder>)const resend = variable('resend')const STORE_FIELDS = [@@ -85,9 +103,10 @@ export const EMAIL_TEMPLATE_VARIABLES: Record<string, EmailTemplateVariable[]> ='order_count','items','fulfillment_groups',- 'display_total',+ 'total',] as const satisfies FieldsOf<EmailOrderGroup>,EmailOrderGroupSchema,+ ['total'] satisfies FieldsOf<EmailOrderGroup>,),resend,],@@ -110,12 +129,9 @@ export const EMAIL_TEMPLATE_VARIABLES: Record<string, EmailTemplateVariable[]> =order,variable('return',- [- 'number',- 'returned_items',- 'display_refunded_total',- ] as const satisfies FieldsOf<EmailReturn>,+ ['number', 'returned_items', 'refunded_total'] as const satisfies FieldsOf<EmailReturn>,EmailReturnSchema,+ ['refunded_total'] satisfies FieldsOf<EmailReturn>,),resend,],diff --git a/spree/core/app/models/spree/fulfillment_group.rb b/spree/core/app/models/spree/fulfillment_group.rbindex 434d55fbb0..715b1bf2ba 100644--- a/spree/core/app/models/spree/fulfillment_group.rb+++ b/spree/core/app/models/spree/fulfillment_group.rb@@ -72,12 +72,17 @@ module Spreefulfillments.sum { |fulfillment| fulfillment.discounted_cost.to_d }end+ # @return [String] the currency the parcel was quoted in+ def currency+ primary.currency+ end+# @return [String, Spree::Money] the forwarder's placeholder while a# freight rate is unpriced, money otherwisedef display_cost(**options)return primary.display_cost(**options) if unpriced?- Spree::Money.new(cost, { currency: primary.currency }.merge(options))+ Spree::Money.new(cost, { currency: currency }.merge(options))end# @return [Boolean]diff --git a/spree/emails/app/serializers/spree/emails/fulfillment_group_serializer.rb b/spree/emails/app/serializers/spree/emails/fulfillment_group_serializer.rbindex 69b4ffd8b9..1dc95ab94f 100644--- a/spree/emails/app/serializers/spree/emails/fulfillment_group_serializer.rb+++ b/spree/emails/app/serializers/spree/emails/fulfillment_group_serializer.rb@@ -7,6 +7,13 @@ module Spreeattributes :name+ # A freight parcel awaiting its forwarder's quote has no cost yet.+ attribute :unpriced, &:unpriced?++ attribute :cost do |group|+ Spree::Money::Rounding.format(group.cost, group.currency)+ end+attribute :display_cost do |group|group.display_cost.to_senddiff --git a/spree/emails/app/serializers/spree/emails/line_item_serializer.rb b/spree/emails/app/serializers/spree/emails/line_item_serializer.rbindex 53e756afd6..9da5850437 100644--- a/spree/emails/app/serializers/spree/emails/line_item_serializer.rb+++ b/spree/emails/app/serializers/spree/emails/line_item_serializer.rb@@ -5,12 +5,16 @@ module Spreemany :option_values, resource: Spree::Emails::OptionValueSerializer- typelize sku: [:string, nullable: true], display_amount: :string,+ typelize sku: [:string, nullable: true], amount: :string, display_amount: :string,url: [:string, nullable: true], image_url: [:string, nullable: true]attributes :sku# Price times quantity, before discounts — what the line cost.+ attribute :amount do |line_item|+ Spree::Money::Rounding.format(line_item.amount, line_item.currency)+ end+attribute :display_amount do |line_item|line_item.display_amount.to_senddiff --git a/spree/emails/app/serializers/spree/emails/parcel_item_serializer.rb b/spree/emails/app/serializers/spree/emails/parcel_item_serializer.rbindex cdd7076241..7d1395e260 100644--- a/spree/emails/app/serializers/spree/emails/parcel_item_serializer.rb+++ b/spree/emails/app/serializers/spree/emails/parcel_item_serializer.rb@@ -24,6 +24,14 @@ module Spreeitem.line_item.options_textend+ attribute :price do |item|+ Spree::Money::Rounding.format(item.line_item.price, item.line_item.currency, unit_price: true)+ end++ attribute :amount do |item|+ Spree::Money::Rounding.format(item.line_item.price * item.quantity, item.line_item.currency)+ end+attribute :display_price do |item|item.line_item.display_price.to_senddiff --git a/spree/emails/app/serializers/spree/emails/purchase_summary_attributes.rb b/spree/emails/app/serializers/spree/emails/purchase_summary_attributes.rbindex 44cabdb68f..657147e2a1 100644--- a/spree/emails/app/serializers/spree/emails/purchase_summary_attributes.rb+++ b/spree/emails/app/serializers/spree/emails/purchase_summary_attributes.rb@@ -7,12 +7,16 @@ module Spreeextend ActiveSupport::Concernincluded do- typelize customer_name: :string, display_total_minus_store_credits: :string+ typelize customer_name: :string, total_minus_store_credits: :string, display_total_minus_store_credits: :stringattribute :customer_name do |purchase|purchase.name.presence || I18n.t('spree.customer')end+ attribute :total_minus_store_credits do |purchase|+ Spree::Money::Rounding.format(purchase.total_minus_store_credits, purchase.currency)+ end+attribute :display_total_minus_store_credits do |purchase|purchase.display_total_minus_store_credits.to_senddiff --git a/spree/emails/app/serializers/spree/emails/return_serializer.rb b/spree/emails/app/serializers/spree/emails/return_serializer.rbindex b35c4efad2..0c157388be 100644--- a/spree/emails/app/serializers/spree/emails/return_serializer.rb+++ b/spree/emails/app/serializers/spree/emails/return_serializer.rb@@ -1,10 +1,14 @@module Spreemodule Emailsclass ReturnSerializer < Spree::Api::V3::ReturnSerializer- typelize display_refunded_total: :string+ typelize refunded_total: :string, display_refunded_total: :string# What was actually paid back — not the Store API's refund_total, which# is the figure the return was expected to refund.+ attribute :refunded_total do |return_record|+ Spree::Money::Rounding.format(return_record.refunded_total, return_record.currency)+ end+attribute :display_refunded_total do |return_record|return_record.display_refunded_total.to_senddiff --git a/spree/emails/app/views/spree/return_mailer/refunded_email.liquid b/spree/emails/app/views/spree/return_mailer/refunded_email.liquidindex 50f58dc58a..db6699643f 100644--- a/spree/emails/app/views/spree/return_mailer/refunded_email.liquid+++ b/spree/emails/app/views/spree/return_mailer/refunded_email.liquid@@ -12,7 +12,8 @@ subject: "{% if resend %}[{{ 'resend' | t }}] {% endif %}{{ store.name }} {{ 're<mj-section padding="48px 0 0"><mj-column><mj-text mj-class="section-heading" padding="0 0 16px">{{ 'return_mailer.refunded_email.refund_summary' | t }}</mj-text>- <mj-text mj-class="body" padding="0" font-weight="500" color="{{ store.branding.heading_color }}">{{ 'return_mailer.refunded_email.total_refunded' | t: total: return.display_refunded_total }}</mj-text>+ {%- assign refunded_total = return.refunded_total | money -%}+ <mj-text mj-class="body" padding="0" font-weight="500" color="{{ store.branding.heading_color }}">{{ 'return_mailer.refunded_email.total_refunded' | t: total: refunded_total }}</mj-text></mj-column></mj-section>{% render 'spree/shared/order_summary', heading: heading, purchase: order, items: return.returned_items, totals: false %}diff --git a/spree/emails/app/views/spree/shared/_fulfillment_group.liquid b/spree/emails/app/views/spree/shared/_fulfillment_group.liquidindex 68783db186..91e357b25d 100644--- a/spree/emails/app/views/spree/shared/_fulfillment_group.liquid+++ b/spree/emails/app/views/spree/shared/_fulfillment_group.liquid@@ -14,7 +14,12 @@{%- endif -%}{% render 'spree/shared/line_item' for group.items as item %}<mj-table css-class="summary" padding="16px 0 0">- {% render 'spree/shared/summary_row', label: 'shipping', translate: true, sub: group.name, value: group.display_cost %}+ {%- if group.unpriced -%}+ {%- assign cost = 'delivery_rates.quoted_after_review' | t -%}+ {%- else -%}+ {%- assign cost = group.cost | money -%}+ {%- endif -%}+ {% render 'spree/shared/summary_row', label: 'shipping', translate: true, sub: group.name, value: cost %}</mj-table></mj-column></mj-section>diff --git a/spree/emails/app/views/spree/shared/_line_item.liquid b/spree/emails/app/views/spree/shared/_line_item.liquidindex da143044b5..577542ff3d 100644--- a/spree/emails/app/views/spree/shared/_line_item.liquid+++ b/spree/emails/app/views/spree/shared/_line_item.liquid@@ -19,8 +19,8 @@{%- if item.options_text != blank -%}<p class="item_meta">{{ item.options_text }}</p>{%- endif -%}</td><td class="item_amount" align="right" valign="middle">- <p class="item_qty">{{ item.quantity }} x {{ item.display_price }}</p>- <p class="item_price">{{ item.display_amount }}</p>+ <p class="item_qty">{{ item.quantity }} x {{ item.price | money }}</p>+ <p class="item_price">{{ item.amount | money }}</p></td></tr></table>diff --git a/spree/emails/app/views/spree/shared/_purchase_totals.liquid b/spree/emails/app/views/spree/shared/_purchase_totals.liquidindex 3a868dbb52..065f95beb2 100644--- a/spree/emails/app/views/spree/shared/_purchase_totals.liquid+++ b/spree/emails/app/views/spree/shared/_purchase_totals.liquid@@ -4,25 +4,32 @@{%- assign tax_total = purchase.additional_tax_total | plus: 0 -%}{%- assign gift_card_total = purchase.gift_card_total | plus: 0 -%}<mj-table css-class="summary" padding="16px 0 0">- {% render 'spree/shared/summary_row', label: 'order_mailer.subtotal', translate: true, value: purchase.display_item_total %}+ {%- assign value = purchase.item_total | money -%}+ {% render 'spree/shared/summary_row', label: 'order_mailer.subtotal', translate: true, value: value %}{%- for line in purchase.promotion_discounts -%}- {% render 'spree/shared/summary_row', label: 'promotion', translate: true, sub: line.label, value: line.display_amount %}+ {%- assign value = line.amount | money -%}+ {% render 'spree/shared/summary_row', label: 'promotion', translate: true, sub: line.label, value: value %}{%- endfor -%}{%- for line in purchase.delivery_lines -%}- {% render 'spree/shared/summary_row', label: 'shipping', translate: true, sub: line.label, value: line.display_amount %}+ {%- assign value = line.amount | money -%}+ {% render 'spree/shared/summary_row', label: 'shipping', translate: true, sub: line.label, value: value %}{%- endfor -%}{%- if tax_total != 0 -%}- {% render 'spree/shared/summary_row', label: 'tax', translate: true, value: purchase.display_additional_tax_total %}+ {%- assign value = purchase.additional_tax_total | money -%}+ {% render 'spree/shared/summary_row', label: 'tax', translate: true, value: value %}{%- endif -%}{%- if gift_card_total > 0 -%}- {%- assign gift_card_value = purchase.display_gift_card_total | prepend: '-' -%}+ {%- assign gift_card_value = purchase.gift_card_total | money | prepend: '-' -%}{% render 'spree/shared/summary_row', label: 'gift_card', translate: true, value: gift_card_value %}{%- endif -%}{%- for line in purchase.manual_discounts -%}- {% render 'spree/shared/summary_row', label: line.label, value: line.display_amount %}+ {%- assign value = line.amount | money -%}+ {% render 'spree/shared/summary_row', label: line.label, value: value %}{%- endfor -%}{%- for line in purchase.fee_lines -%}- {% render 'spree/shared/summary_row', label: line.label, value: line.display_amount %}+ {%- assign value = line.amount | money -%}+ {% render 'spree/shared/summary_row', label: line.label, value: value %}{%- endfor -%}- {% render 'spree/shared/summary_row', label: 'order_mailer.total', translate: true, value: purchase.display_total_minus_store_credits, total: true %}+ {%- assign value = purchase.total_minus_store_credits | money -%}+ {% render 'spree/shared/summary_row', label: 'order_mailer.total', translate: true, value: value, total: true %}</mj-table>diff --git a/spree/emails/config/initializers/typelizer.rb b/spree/emails/config/initializers/typelizer.rbindex 7d3699bcbb..e44a4a7f4c 100644--- a/spree/emails/config/initializers/typelizer.rb+++ b/spree/emails/config/initializers/typelizer.rb@@ -13,14 +13,16 @@ Rails.application.config.after_initialize doendSpree::Emails::AmountLineSerializer.typelize(label: [:string, nullable: true], amount: :string, display_amount: :string)- Spree::Emails::FulfillmentGroupSerializer.typelize(name: [:string, nullable: true], display_cost: :string,+ Spree::Emails::FulfillmentGroupSerializer.typelize(name: [:string, nullable: true], unpriced: :boolean, cost: :string,+ display_cost: :string,seller_names: [:string, multi: true],# Declared before Typelizer's hooks were attached, so it# cannot tell this association is a list.items: [:EmailParcelItem, multi: true])Spree::Emails::ParcelItemSerializer.typelize(id: :string, name: :string, quantity: :number, sku: [:string, nullable: true], options_text: [:string, nullable: true],- display_price: :string, display_amount: :string, url: [:string, nullable: true], image_url: [:string, nullable: true]+ price: :string, amount: :string, display_price: :string, display_amount: :string, url: [:string, nullable: true],+ image_url: [:string, nullable: true])Spree::Emails::StoreSerializer.typelize(id: :string, name: :string, address: [:string, nullable: true], mail_from_address: :string, default_currency: :string,diff --git a/spree/emails/spec/mailers/spree/order_group_mailer_spec.rb b/spree/emails/spec/mailers/spree/order_group_mailer_spec.rbindex 160b57fa22..3e0ad3e37c 100644--- a/spree/emails/spec/mailers/spree/order_group_mailer_spec.rb+++ b/spree/emails/spec/mailers/spree/order_group_mailer_spec.rb@@ -106,6 +106,16 @@ describe Spree::OrderGroupMailer, type: :mailer doexpect(quoted).to eq(group.delivery_total)expect(message.text_part.body.to_s).to include(group.display_delivery_total.to_s)end++ # A freight parcel awaiting its forwarder's quote costs zero for now;+ # calling that free is what the email must never do.+ it 'says a parcel awaiting a quote is quoted after review' do+ group.orders.first.fulfillments.first.selected_delivery_rate.update_columns(unpriced: true)++ parts_of(described_class.confirm_email(group.reload)).each do |body|+ expect(body).to include('Quoted after review')+ end+ endend# SplitBySeller apportions an order-level fee into one row per child, all