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
Fix: cart metadata was lost when the cart was placed as an order (#14705)
Damian Legawiec8117404754da
diff --git a/docs/developer/core-concepts/carts.mdx b/docs/developer/core-concepts/carts.mdxindex 25ecd99c01..f02cacf99c 100644--- a/docs/developer/core-concepts/carts.mdx+++ b/docs/developer/core-concepts/carts.mdx@@ -292,6 +292,12 @@ Completing a cart moves real money, so Spree is careful about it:**Totals are worked out at the last moment.** The amount charged is calculated when the cart is completed, not taken from an earlier request — so a price or promotion that changed while the customer was reviewing can't lead to the wrong charge.+**The order keeps what the cart collected.** The customer, addresses, items, notes, purchase order number, tax identifier and the cart's `metadata` are all copied onto the order. When a checkout is split into one order per seller, every one of those orders receives the same copy. After that the cart and the order are independent — changing one never changes the other.++<Warning>+ A customer can write a cart's `metadata` through the Store API, so once it is copied onto the order, treat those keys as customer input. Do not keep anything there that your own code later trusts, such as a fraud or approval flag.+</Warning>+If a cart can't be completed you get told why — a payment failure, or unmet [requirements](#checkout-requirements) — and the cart is left alone so the customer can fix it and try again.## Abandoned cartsdiff --git a/docs/developer/core-concepts/orders.mdx b/docs/developer/core-concepts/orders.mdxindex 33c32fd22e..2e9ea9df09 100644--- a/docs/developer/core-concepts/orders.mdx+++ b/docs/developer/core-concepts/orders.mdx@@ -5,7 +5,7 @@ description: How Spree models orders — a permanent record created by completin## Overview-An order is a placed purchase. It's created by completing a [Cart](/developer/core-concepts/carts) and holds its own copy of everything the customer agreed to — items, prices, taxes, discounts and delivery.+An order is a placed purchase. It's created by completing a [Cart](/developer/core-concepts/carts) and holds its own copy of everything the customer agreed to — items, prices, taxes, discounts and delivery. Anything your storefront stored in the cart's `metadata` is copied onto the order too, including onto every order of a checkout split between sellers.```mermaiderDiagramdiff --git a/docs/developer/customization/checkout.mdx b/docs/developer/customization/checkout.mdxindex 3499adbc01..5fb9e58db8 100644--- a/docs/developer/customization/checkout.mdx+++ b/docs/developer/customization/checkout.mdx@@ -43,6 +43,11 @@ Spree::Checkout::Registry.add_requirement(While `satisfied:` returns false the cart reports the requirement and refuses tocomplete. Its `code` is derived as `<field>_required` — `vat_number_required` here.+The storefront satisfies it by writing the value into the cart's `metadata`. When the+cart is completed, its `metadata` is copied onto the order — onto every order if the+checkout is split between sellers — so the VAT number is still there after placement as+`order.metadata['vat_number']`.+Pass `applicable:` to scope it. It is checked before `satisfied:`, so a requirementthat doesn't apply never appears at all:diff --git a/spree/core/app/services/spree/carts/split_by_seller.rb b/spree/core/app/services/spree/carts/split_by_seller.rbindex 5c061fa1ef..61fc58f1be 100644--- a/spree/core/app/services/spree/carts/split_by_seller.rb+++ b/spree/core/app/services/spree/carts/split_by_seller.rb@@ -29,10 +29,15 @@ module Spree# blocklist would carry every future column onto siblings by default.# Adding a column here is a decision that it describes the checkout# rather than one seller's part of it.+ #+ # +metadata+ is here because at this point it holds what the checkout+ # recorded about the purchase (copied from the cart), so every child gets+ # the same hash — and so does the group. Once split, each order's+ # metadata is its own: edits never propagate between siblings.CARRIED_TO_SIBLING = %w[email currency locale market_id channel_id company_idcustomer_id token accept_marketing preferred_stock_location_id- customer_note last_ip_address po_number+ customer_note last_ip_address po_number metadata].freeze# @param order [Spree::Order] the paid draft order, adopted as the@@ -91,6 +96,7 @@ module Spreecurrency: order.currency,email: order.email,token: order.token,+ metadata: order.metadata.to_h.deep_dup,ship_address: order.ship_address&.snapshot,bill_address: order.bill_address&.snapshot)@@ -113,7 +119,7 @@ module Spree# settled during completion.def hand_off(source_order, group, partition, index)sibling = source_order.store.orders.create!(- source_order.attributes.slice(*CARRIED_TO_SIBLING).merge(+ source_order.attributes.slice(*CARRIED_TO_SIBLING).deep_dup.merge('order_group_id' => group.id,'seller_id' => partition.seller_id,'number' => "#{group.number}-#{index}",diff --git a/spree/core/app/workflows/spree/carts/complete.rb b/spree/core/app/workflows/spree/carts/complete.rbindex af8b499628..49247db90f 100644--- a/spree/core/app/workflows/spree/carts/complete.rb+++ b/spree/core/app/workflows/spree/carts/complete.rb@@ -207,6 +207,13 @@ module Spree# typed money lines re-pointed, fulfillments + selected rates, address# copies); money records (payments, sessions, reservations, coupon# codes) re-point — external transaction references must never fork.+ #+ # The cart's metadata is copied whole: storefronts and checkout+ # requirements keep what they collected about the purchase there, and it+ # has to outlive the cart. The order is new at this point, so there is+ # nothing on it to merge with — the cart's hash simply becomes the+ # order's. A deep copy, so a later edit to either record can never reach+ # the other through a shared nested hash.def create_draft_order!(cart)order = nilApplicationRecord.transaction do@@ -228,6 +235,7 @@ module Spreepo_number: cart.po_number,gift_card: cart.gift_card,last_ip_address: cart.last_ip_address,+ metadata: cart.metadata.to_h.deep_dup,ship_address: cart.ship_address&.snapshot,bill_address: cart.bill_address&.snapshot)diff --git a/spree/core/spec/workflows/spree/carts/complete_spec.rb b/spree/core/spec/workflows/spree/carts/complete_spec.rbindex 20c2b5d66c..3192c46a3d 100644--- a/spree/core/spec/workflows/spree/carts/complete_spec.rb+++ b/spree/core/spec/workflows/spree/carts/complete_spec.rb@@ -383,6 +383,31 @@ module Spreeendend+ describe 'cart metadata' do+ it 'carries the cart metadata onto the order' do+ ready_cart.update!(metadata: { 'gift_message' => 'Happy birthday', 'delivery' => { 'window' => 'am' } })++ order = described_class.call(cart: ready_cart).value++ expect(order.reload.metadata).to eq('gift_message' => 'Happy birthday', 'delivery' => { 'window' => 'am' })+ end++ it 'gives the order its own copy rather than sharing the cart hash' do+ ready_cart.update!(metadata: { 'delivery' => { 'window' => 'am' } })++ order = described_class.call(cart: ready_cart).value+ order.metadata['delivery']['window'] = 'pm'++ expect(ready_cart.metadata['delivery']['window']).to eq('am')+ end++ it 'leaves the order metadata empty when the cart has none' do+ order = described_class.call(cart: ready_cart).value++ expect(order.reload.metadata).to eq({})+ end+ end+describe 'the buyer purchase order' doit 'carries the reference onto the order' doready_cart.update!(po_number: 'PO-4471')diff --git a/spree/core/spec/workflows/spree/carts/complete_split_spec.rb b/spree/core/spec/workflows/spree/carts/complete_split_spec.rbindex def97b5dd3..b4a74197f3 100644--- a/spree/core/spec/workflows/spree/carts/complete_split_spec.rb+++ b/spree/core/spec/workflows/spree/carts/complete_split_spec.rb@@ -126,6 +126,21 @@ module Spreeendend+ context 'when the cart carries metadata' do+ before { cart.update!(metadata: { 'gift_message' => 'Happy birthday' }) }++ it 'carries it onto every child order' do+ expect(group.orders.size).to eq(3)+ group.orders.each do |order|+ expect(order.reload.metadata).to eq('gift_message' => 'Happy birthday')+ end+ end++ it 'carries it onto the group' do+ expect(group.reload.metadata).to eq('gift_message' => 'Happy birthday')+ end+ end+it 'computes each child’s totals from its own rows' dogroup.orders.each do |order|expect(order.total).to be > 0