用途:在 Spree 6 中处理定价。价格始终是「变体 + 金额 + 币种」,没有隐含的单一价格;基础价格之上可叠加价格表(条件覆盖),按请求的定价上下文解析。
主要能力:基础价格用 variant.set_price('USD', 39.99)、variant.price_in('USD')、variant.amount_in('USD') 读写;set_price 第三参数为 compare_at_amount,省略会清空比较价,金额为空则删除该币种价格。无汇率换算,每个销售币种需单独定价;缺少请求币种基础价格的商品不会出现在 Store API 列表中。价格表(pl_)支持 draft/active/scheduled/inactive 状态、starts_at/ends_at 时间窗、match_policy、position 优先级,以及目录归属的百分比调整。价格规则包括 market_rule、channel_rule、volume_rule 等,可自定义子类并注册。解析顺序为:目录归属列表(就近节点、同节点取最低价)→ 按 position 匹配的独立列表 → 基础价格。数量阶梯价可用 min_quantity 价格行或 volume_rule 实现。基础价格变更写入 PriceHistory,支持 prior_price 与 EU Omnibus。
使用方式:Admin API 提供 admin.prices.create、bulkUpsert、bulkDestroy,以及 admin.priceLists.create/activate;命令行示例:spree api post /prices/bulk_upsert -d '{"prices":[{"variant_id":"variant_xxx","currency":"USD","amount":"15.99"}]}'。Store API 自动从请求头与客户构建定价上下文。
限制与注意:update_all 跳过回调,不写价格历史;set_price 省略第三参数会清空比较价;price_in 返回未保存的空 Price 而非 nil;展开的 prices 数组回写会被当作基础价格,需先过滤 price_list_id 为 null;min_quantity > 1 必须带 price_list_id;Store API 价格区间仅基于基础价格。结账折扣与优惠券应使用促销而非价格表。