Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Fix: customer.carts returns the customer's open Spree::Cart records (#14698)

Damian Legawieca56f8bb43ef1
diff --git a/spree/core/app/models/concerns/spree/customer_methods.rb b/spree/core/app/models/concerns/spree/customer_methods.rb
index 5b74449a94..7a41f0a4f7 100644
--- a/spree/core/app/models/concerns/spree/customer_methods.rb
+++ b/spree/core/app/models/concerns/spree/customer_methods.rb
@@ -64,7 +64,14 @@ module Spree
has_many :promotion_rule_users, class_name: 'Spree::PromotionRuleUser', foreign_key: :customer_id, dependent: :destroy
has_many :promotion_rules, through: :promotion_rule_users, class_name: 'Spree::PromotionRule'
has_many :orders, foreign_key: :customer_id, class_name: 'Spree::Order'
- has_many :carts, -> { incomplete }, foreign_key: :customer_id, class_name: 'Spree::Order'
+ # Open carts only. Since the Cart/Order split a cart lives on its own
+ # table and never becomes an order row, so an incomplete Order is a
+ # backoffice draft, not a cart. Completed carts stay behind as read-only
+ # audit records of a checkout and are reached through the order.
+ # No cascade on destroy, like #orders: a cart can be mid-checkout with
+ # money authorized against it, and erasure goes through
+ # Spree::Customers::Anonymize, which scrubs carts explicitly.
+ has_many :carts, -> { incomplete }, foreign_key: :customer_id, class_name: 'Spree::Cart', dependent: nil
has_many :completed_orders, -> { complete }, foreign_key: :customer_id, class_name: 'Spree::Order'
has_many :store_credits, class_name: 'Spree::StoreCredit', foreign_key: :customer_id, dependent: :destroy
# Everything the customer has bought and can download. Scoped to completed
@@ -272,11 +279,20 @@ module Spree
end
# Returns the last incomplete spree order for the current store
+ # @deprecated Carts are {Spree::Cart} since 6.0, so an incomplete order is
+ # a backoffice draft rather than the customer's cart. Use
+ # +carts.where(store: store)+ for carts, or +orders.drafts+ for drafts.
+ # Removed in Spree 6.1.
# @param [Spree::Store] store
# @param [Hash] options
# @option options [Array<Symbol>] :includes
# @return [Spree::Order]
def last_incomplete_spree_order(store, options = {})
+ Spree::Deprecation.warn(
+ 'Spree::CustomerMethods#last_incomplete_spree_order is deprecated and will be removed in Spree 6.1. ' \
+ 'Carts are Spree::Cart since 6.0 and this returns draft orders; use #carts for the customer\'s carts.'
+ )
+
orders.where(store: store).incomplete.not_canceled.
includes(options[:includes]).
order('created_at DESC').
diff --git a/spree/core/spec/models/spree/concerns/user_methods_spec.rb b/spree/core/spec/models/spree/concerns/user_methods_spec.rb
index 8a23e93f2e..42896f7ed3 100644
--- a/spree/core/spec/models/spree/concerns/user_methods_spec.rb
+++ b/spree/core/spec/models/spree/concerns/user_methods_spec.rb
@@ -5,9 +5,36 @@ describe Spree::UserMethods do
let!(:another_user) { create(:user) }
let(:current_store) { @default_store }
+ describe '#carts' do
+ let!(:open_cart) { create(:cart, customer: test_user, store: current_store) }
+
+ before do
+ create(:cart, customer: another_user, store: current_store)
+ create(:cart, customer: test_user, store: current_store, completed_at: Time.current)
+ # A backoffice draft order is incomplete but it is not a cart.
+ create(:order, customer: test_user, store: current_store)
+ end
+
+ it 'returns only the customer\'s open Spree::Cart records' do
+ expect(test_user.carts).to contain_exactly(open_cart)
+ expect(test_user.carts.first).to be_a(Spree::Cart)
+ end
+
+ it 'leaves carts in place when the customer is destroyed' do
+ expect { test_user.destroy }.not_to change(Spree::Cart, :count)
+ end
+ end
+
describe '#last_incomplete_spree_order' do
subject { test_user.last_incomplete_spree_order(current_store) }
+ before { allow(Spree::Deprecation).to receive(:warn) }
+
+ it 'is deprecated' do
+ subject
+ expect(Spree::Deprecation).to have_received(:warn).with(/last_incomplete_spree_order is deprecated/)
+ end
+
context 'with an incomplete order' do
let(:last_incomplete_order) { create :order, customer: test_user, store: current_store }
diff --git a/spree/core/spec/models/spree/user_spec.rb b/spree/core/spec/models/spree/user_spec.rb
index a15c81ddbe..6c18625334 100644
--- a/spree/core/spec/models/spree/user_spec.rb
+++ b/spree/core/spec/models/spree/user_spec.rb
@@ -50,6 +50,8 @@ describe Spree.customer_class, type: :model do # rubocop:disable RSpec/MultipleD
let(:order_3) { create(:order, customer: user, created_by: create(:admin_user), store: store) }
it 'returns correct order' do
+ allow(Spree::Deprecation).to receive(:warn)
+
Timecop.scale(3600) do
order_1
order_2