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
Consolidate cart schema and standardize totals naming
Damian Legawiec9438d93b496b
diff --git a/spree/core/app/models/spree/cart.rb b/spree/core/app/models/spree/cart.rbindex 938a45805d..10abe8401d 100644--- a/spree/core/app/models/spree/cart.rb+++ b/spree/core/app/models/spree/cart.rb@@ -23,28 +23,15 @@ module Spreeattribute :metadata, default: -> { {} }attribute :accept_marketing, :boolean, default: false- with_options default: 0.0 do- attribute :item_total, :decimal- attribute :adjustment_total, :decimal- attribute :included_tax_total, :decimal- attribute :additional_tax_total, :decimal- attribute :taxable_adjustment_total, :decimal- attribute :non_taxable_adjustment_total, :decimal- attribute :promo_total, :decimal- attribute :fee_total, :decimal- attribute :delivery_total, :decimal- attribute :total, :decimal- attribute :payment_total, :decimal- end- attribute :item_count, :integer, default: 0- attribute :lock_version, :integer, default: 0- alias_attribute :total_quantity, :item_count- alias_attribute :discount_total, :promo_total+ # Standardized column names; legacy readers stay as aliases one release.+ alias_attribute :item_count, :total_quantity+ alias_attribute :promo_total, :discount_totalextend Spree::DisplayMoneymoney_methods :item_total, :adjustment_total, :included_tax_total, :additional_tax_total,- :promo_total, :fee_total, :delivery_total, :total, :payment_total, :outstanding_balance,- :discount_total, :tax_total, :pre_tax_item_amount, :pre_tax_total, :amount_due+ :discount_total, :fee_total, :delivery_total, :total, :payment_total, :outstanding_balance,+ :tax_total, :pre_tax_item_amount, :pre_tax_total, :amount_due+ alias display_promo_total display_discount_totalalias display_ship_total display_delivery_totalalias_attribute :ship_total, :delivery_total@@ -55,6 +42,16 @@ module Spreebelongs_to :ship_address, class_name: 'Spree::Address', optional: true, dependent: :destroybelongs_to :bill_address, class_name: 'Spree::Address', optional: true, dependent: :destroybelongs_to :gift_card, class_name: 'Spree::GiftCard', optional: true+ # Normalizes like the legacy Order writer so lookups stay case-insensitive.+ def coupon_code=(code)+ normalized = begin+ code.strip.downcase+ rescue StandardError+ nil+ end+ super(normalized)+ end+# Pickup location choice is a preference (mirrors the admin-user# preference of the same name), copied to the order column at completion.preference :stock_location_id, :integer, nullable: truediff --git a/spree/core/app/models/spree/cart_updater.rb b/spree/core/app/models/spree/cart_updater.rbindex 532d886c26..9bb6af6879 100644--- a/spree/core/app/models/spree/cart_updater.rb+++ b/spree/core/app/models/spree/cart_updater.rb@@ -22,7 +22,7 @@ module Spreedef persist_totalscart.update_columns(item_total: cart.item_total,- item_count: cart.item_count,+ total_quantity: cart.total_quantity,adjustment_total: cart.adjustment_total,included_tax_total: cart.included_tax_total,additional_tax_total: cart.additional_tax_total,@@ -30,7 +30,7 @@ module Spreenon_taxable_adjustment_total: cart.non_taxable_adjustment_total,payment_total: cart.payment_total,delivery_total: cart.delivery_total,- promo_total: cart.promo_total,+ discount_total: cart.discount_total,fee_total: cart.fee_total,total: cart.total,updated_at: Time.current@@ -53,7 +53,7 @@ module Spreecart_fees = cart.fees.reload.to_atax_sums = cart.tax_lines.reload.group_by(&:included?).transform_values { |lines| lines.sum(&:amount) }- cart.promo_total = cart.discounts.select(&:promotion?).sum(&:amount)+ cart.discount_total = cart.discounts.select(&:promotion?).sum(&:amount)cart.fee_total = cart_fees.sum(&:amount)cart.included_tax_total = tax_sums.fetch(true, 0)cart.additional_tax_total = tax_sums.fetch(false, 0)diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rbindex 162b473229..9af889172a 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -56,7 +56,7 @@ module Spreemoney_methods :outstanding_balance, :item_total, :adjustment_total,:included_tax_total, :additional_tax_total, :tax_total,- :delivery_total, :promo_total, :total,+ :delivery_total, :discount_total, :total,:cart_promo_total, :pre_tax_item_amount, :pre_tax_total,:payment_total, :amount_due@@ -83,11 +83,12 @@ module Spreeorderend- # 5.5 API naming bridges (DB columns rename in 6.0)- alias_attribute :discount_total, :promo_total- alias display_discount_total display_promo_total+ # Standardized column names (renamed in 6.0); legacy readers stay as+ # aliases one release.+ alias_attribute :promo_total, :discount_total+ alias display_promo_total display_discount_totalalias_attribute :customer_note, :special_instructions- alias_attribute :total_quantity, :item_count+ alias_attribute :item_count, :total_quantityMONEY_THRESHOLD = 100_000_000MONEY_VALIDATION = {@@ -116,11 +117,10 @@ module Spreeself.whitelisted_ransackable_associations = %w[fulfillments shipments user created_by approver canceler promotions bill_address ship_address line_items store channel tags]self.whitelisted_ransackable_attributes = %w[completed_at email number status payment_status payment_state fulfillment_status shipment_state delivery_total- total item_total item_count considered_risky channel_id currency+ total item_total total_quantity considered_risky channel_id currency coupon_code]self.whitelisted_ransackable_scopes = %w[complete incomplete refunded partially_refunded search multi_search]- attr_reader :coupon_codeattr_accessor :temporary_address# Set to false on admin-initiated flows to suppress customer-facing emails.@@ -253,7 +253,7 @@ module Spree# we want to have this case_sentive: true as changing it to false causes all SQL to use LOWER(slug)# which is very costly and slow on large set of recordsvalidates :email, length: { maximum: 254, allow_blank: true }, email: { allow_blank: true }, if: :require_email- validates :item_count, numericality: { greater_than_or_equal_to: 0, less_than: 2**31, only_integer: true, allow_blank: true }+ validates :total_quantity, numericality: { greater_than_or_equal_to: 0, less_than: 2**31, only_integer: true, allow_blank: true }validates :storevalidates :currencyvalidates :locale@@ -266,7 +266,7 @@ module Spreevalidates :additional_tax_total, POSITIVE_MONEY_VALIDATIONvalidates :payment_total, MONEY_VALIDATIONvalidates :delivery_total, MONEY_VALIDATION- validates :promo_total, NEGATIVE_MONEY_VALIDATION+ validates :discount_total, NEGATIVE_MONEY_VALIDATIONvalidates :total, MONEY_VALIDATIONvalidate :currency_must_be_supported_by_storevalidate :locale_must_be_supported_by_store@@ -449,7 +449,7 @@ module Spree# Checks if the order is fully refunded# @return [Boolean]def order_refunded?- return false if item_count.zero?+ return false if total_quantity.zero?return false if refunds_total.zero?payment_state.in?(%w[void failed]) || refunds_total == total_minus_store_credits - additional_tax_total.abs@@ -462,7 +462,7 @@ module Spree# Checks if the order is partially refunded# @return [Boolean]def partially_refunded?- return false if item_count.zero?+ return false if total_quantity.zero?return false if payment_state.in?(%w[void failed]) || refunds.empty?refunds_total < total_minus_store_credits - additional_tax_total.abs@@ -806,11 +806,12 @@ module Spreeenddef coupon_code=(code)- @coupon_code = begin+ normalized = begincode.strip.downcaserescue StandardErrornilend+ super(normalized)enddef can_add_coupon?diff --git a/spree/core/app/models/spree/order/digital.rb b/spree/core/app/models/spree/order/digital.rbindex 70ed9b1b17..9ca8f3c809 100644--- a/spree/core/app/models/spree/order/digital.rb+++ b/spree/core/app/models/spree/order/digital.rb@@ -5,7 +5,7 @@ module Spree## @return [Boolean]def digital?- if item_count.zero? || line_items.empty?+ if total_quantity.zero? || line_items.empty?falseelseline_items.includes(variant: :product).all?(&:digital?)@@ -16,7 +16,7 @@ module Spree## @return [Boolean]def some_digital?- if item_count.zero? || line_items.empty?+ if total_quantity.zero? || line_items.empty?falseelseline_items.includes(variant: :product).any?(&:digital?)@@ -27,7 +27,7 @@ module Spree## @return [Boolean]def with_digital_assets?- if item_count.zero? || line_items.empty?+ if total_quantity.zero? || line_items.empty?falseelseline_items.includes(:variant).any?(&:with_digital_assets?)diff --git a/spree/core/app/models/spree/order_updater.rb b/spree/core/app/models/spree/order_updater.rbindex 98b5e9eb34..2060dab30d 100644--- a/spree/core/app/models/spree/order_updater.rb+++ b/spree/core/app/models/spree/order_updater.rb@@ -58,7 +58,7 @@ module Spree# +payment_total+ The total value of all finalized Payments (NOTE: non-finalized Payments are excluded)# +item_total+ The total value of all LineItems# +adjustment_total+ The total value of all typed adjustment rows (discounts, fees, additional tax)- # +promo_total+ The total value of all promotion discounts+ # +discount_total+ The total value of all promotion discounts# +fee_total+ The total value of all fees# +total+ The so-called "order total." Equivalent to +item_total+ plus +delivery_total+ plus +adjustment_total+.def update_totals@@ -125,7 +125,7 @@ module Spreeenddef update_item_count- order.item_count = quantity+ order.total_quantity = quantityenddef sum_typed_rows_into_order@@ -133,7 +133,7 @@ module Spreefees = order.fees.reload.to_atax_sums = order.tax_lines.reload.group_by(&:included?).transform_values { |lines| lines.sum(&:amount) }- order.promo_total = order.discounts.select(&:promotion?).sum(&:amount)+ order.discount_total = order.discounts.select(&:promotion?).sum(&:amount)order.fee_total = fees.sum(&:amount)order.included_tax_total = tax_sums.fetch(true, 0)order.additional_tax_total = tax_sums.fetch(false, 0)@@ -155,7 +155,7 @@ module Spreepayment_status: order.payment_status,fulfillment_status: order.fulfillment_status,item_total: order.item_total,- item_count: order.item_count,+ total_quantity: order.total_quantity,adjustment_total: order.adjustment_total,included_tax_total: order.included_tax_total,additional_tax_total: order.additional_tax_total,@@ -163,7 +163,7 @@ module Spreenon_taxable_adjustment_total: order.non_taxable_adjustment_total,payment_total: order.payment_total,delivery_total: order.delivery_total,- promo_total: order.promo_total,+ discount_total: order.discount_total,fee_total: order.fee_total,total: order.total,updated_at: Time.currentdiff --git a/spree/core/app/models/spree/payment/gateway_options.rb b/spree/core/app/models/spree/payment/gateway_options.rbindex 55d0f8b368..555d7c678b 100644--- a/spree/core/app/models/spree/payment/gateway_options.rb+++ b/spree/core/app/models/spree/payment/gateway_options.rb@@ -51,7 +51,7 @@ module Spreeenddef discount- order.promo_total * exchange_multiplier+ order.discount_total * exchange_multiplierenddef billing_addressdiff --git a/spree/core/app/models/spree/report_line_items/sales_total.rb b/spree/core/app/models/spree/report_line_items/sales_total.rbindex d2f7b6ea4b..2c8819a52a 100644--- a/spree/core/app/models/spree/report_line_items/sales_total.rb+++ b/spree/core/app/models/spree/report_line_items/sales_total.rb@@ -30,7 +30,7 @@ module Spreeenddef promo_total- record.display_promo_total+ record.display_discount_totalenddef pre_tax_amountdiff --git a/spree/core/app/models/spree/return_authorization.rb b/spree/core/app/models/spree/return_authorization.rbindex b5fbdd1f69..d0b7e2bf13 100644--- a/spree/core/app/models/spree/return_authorization.rb+++ b/spree/core/app/models/spree/return_authorization.rb@@ -59,7 +59,7 @@ module Spreeenddef refundable_amount- order.pre_tax_item_amount + order.promo_total+ order.pre_tax_item_amount + order.discount_totalenddef customer_returned_items?diff --git a/spree/core/app/presenters/spree/csv/order_line_item_presenter.rb b/spree/core/app/presenters/spree/csv/order_line_item_presenter.rbindex de82b537f7..35f99ecae6 100644--- a/spree/core/app/presenters/spree/csv/order_line_item_presenter.rb+++ b/spree/core/app/presenters/spree/csv/order_line_item_presenter.rb@@ -78,9 +78,9 @@ module Spreeindex.zero? ? order.shipment_total.to_f : nil,index.zero? ? order.tax_total.to_f : nil,index.zero? ? order.included_tax_total.positive? : nil,- index.zero? ? (order.promo_total.negative? || line_item.promo_total.negative?) : nil,+ index.zero? ? (order.discount_total.negative? || line_item.promo_total.negative?) : nil,index.zero? ? order.has_free_shipping? : nil,- index.zero? ? order.promo_total.abs : nil,+ index.zero? ? order.discount_total.abs : nil,index.zero? ? order.promo_code : nil,index.zero? ? order.payments.store_credits.sum(:amount).abs : nil,index.zero? ? order.total.to_f : nil,diff --git a/spree/core/app/services/spree/carts/remove_out_of_stock_items.rb b/spree/core/app/services/spree/carts/remove_out_of_stock_items.rbindex a21eb36703..5d11b22e01 100644--- a/spree/core/app/services/spree/carts/remove_out_of_stock_items.rb+++ b/spree/core/app/services/spree/carts/remove_out_of_stock_items.rb@@ -7,7 +7,7 @@ module Spree@messages = []@warnings = []- return success([order, @messages, @warnings]) if order.item_count.zero? || order.line_items.none?+ return success([order, @messages, @warnings]) if order.total_quantity.zero? || order.line_items.none?line_items = order.line_items.includes(variant: [:product, :stock_locations, { stock_items: [:stock_location, :active_stock_reservations] }])diff --git a/spree/core/db/migrate/20260728000003_create_spree_carts.rb b/spree/core/db/migrate/20260728000003_create_spree_carts.rbindex df77a05b2d..92b72bb316 100644--- a/spree/core/db/migrate/20260728000003_create_spree_carts.rb+++ b/spree/core/db/migrate/20260728000003_create_spree_carts.rb@@ -2,8 +2,8 @@ class CreateSpreeCarts < ActiveRecord::Migration[7.2]def changecreate_table :spree_carts do |t|t.references :store, null: false- t.references :market, null: true- t.references :channel, null: true+ t.references :market, null: false+ t.references :channel, null: falset.references :customer, null: truet.string :currency, null: falset.string :locale@@ -14,9 +14,24 @@ class CreateSpreeCarts < ActiveRecord::Migration[7.2]t.text :special_instructionst.string :last_ip_addresst.string :token, null: false- t.integer :lock_version+ t.integer :lock_version, default: 0t.datetime :completed_att.datetime :completing_at+ t.string :coupon_code+ t.references :gift_card, null: true++ t.decimal :item_total, precision: 10, scale: 2, default: 0.0+ t.decimal :adjustment_total, precision: 10, scale: 2, default: 0.0+ t.decimal :included_tax_total, precision: 10, scale: 2, default: 0.0+ t.decimal :additional_tax_total, precision: 10, scale: 2, default: 0.0+ t.decimal :taxable_adjustment_total, precision: 10, scale: 2, default: 0.0+ t.decimal :non_taxable_adjustment_total, precision: 10, scale: 2, default: 0.0+ t.decimal :discount_total, precision: 10, scale: 2, default: 0.0+ t.decimal :fee_total, precision: 10, scale: 2, default: 0.0+ t.decimal :delivery_total, precision: 10, scale: 2, default: 0.0+ t.decimal :total, precision: 10, scale: 2, default: 0.0+ t.decimal :payment_total, precision: 10, scale: 2, default: 0.0+ t.integer :total_quantity, default: 0if t.respond_to?(:jsonb)t.jsonb :metadataelsediff --git a/spree/core/db/migrate/20260729100001_add_checkout_columns_to_spree_carts.rb b/spree/core/db/migrate/20260729100001_add_checkout_columns_to_spree_carts.rbindex c7b84fe62e..92fca6a284 100644--- a/spree/core/db/migrate/20260729100001_add_checkout_columns_to_spree_carts.rb+++ b/spree/core/db/migrate/20260729100001_add_checkout_columns_to_spree_carts.rb@@ -1,21 +1,5 @@class AddCheckoutColumnsToSpreeCarts < ActiveRecord::Migration[7.2]def change- change_table :spree_carts do |t|- t.decimal :item_total, precision: 10, scale: 2, default: 0.0- t.decimal :adjustment_total, precision: 10, scale: 2, default: 0.0- t.decimal :included_tax_total, precision: 10, scale: 2, default: 0.0- t.decimal :additional_tax_total, precision: 10, scale: 2, default: 0.0- t.decimal :taxable_adjustment_total, precision: 10, scale: 2, default: 0.0- t.decimal :non_taxable_adjustment_total, precision: 10, scale: 2, default: 0.0- t.decimal :promo_total, precision: 10, scale: 2, default: 0.0- t.decimal :fee_total, precision: 10, scale: 2, default: 0.0- t.decimal :delivery_total, precision: 10, scale: 2, default: 0.0- t.decimal :total, precision: 10, scale: 2, default: 0.0- t.decimal :payment_total, precision: 10, scale: 2, default: 0.0- t.integer :item_count, default: 0- t.string :coupon_code- end-# Owner pattern: promotion connections and payment sessions are created# during checkout (cart-owned), copied/re-pointed at completion.add_reference :spree_order_promotions, :cart, null: truediff --git a/spree/core/db/migrate/20260729100003_add_gift_card_to_spree_carts.rb b/spree/core/db/migrate/20260729100003_add_gift_card_to_spree_carts.rbdeleted file mode 100644index 775bbf9d96..0000000000--- a/spree/core/db/migrate/20260729100003_add_gift_card_to_spree_carts.rb+++ /dev/null@@ -1,5 +0,0 @@-class AddGiftCardToSpreeCarts < ActiveRecord::Migration[7.2]- def change- add_reference :spree_carts, :gift_card, null: true- end-enddiff --git a/spree/core/db/migrate/20260729120001_rename_order_totals_to_standard_names.rb b/spree/core/db/migrate/20260729120001_rename_order_totals_to_standard_names.rbnew file mode 100644index 0000000000..88e4e6c9cd--- /dev/null+++ b/spree/core/db/migrate/20260729120001_rename_order_totals_to_standard_names.rb@@ -0,0 +1,6 @@+class RenameOrderTotalsToStandardNames < ActiveRecord::Migration[7.2]+ def change+ rename_column :spree_orders, :promo_total, :discount_total+ rename_column :spree_orders, :item_count, :total_quantity+ end+enddiff --git a/spree/core/spec/models/spree/payment/gateway_options_spec.rb b/spree/core/spec/models/spree/payment/gateway_options_spec.rbindex 4cfa5a309e..ad57d8d25e 100644--- a/spree/core/spec/models/spree/payment/gateway_options_spec.rb+++ b/spree/core/spec/models/spree/payment/gateway_options_spec.rb@@ -30,7 +30,7 @@ RSpec.describe Spree::Payment::GatewayOptions, type: :model doship_total: '12.44'.to_d,additional_tax_total: '1.53'.to_d,item_total: '15.11'.to_d,- promo_total: '2.57'.to_d,+ discount_total: '2.57'.to_d,bill_address: bill_address,ship_address: ship_address)