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
Remove a line item by upserting quantity zero
Damian Legawieccd626674926d
diff --git a/packages/admin-sdk/src/params.ts b/packages/admin-sdk/src/params.tsindex 63760bd8c3..1c911bbf78 100644--- a/packages/admin-sdk/src/params.ts+++ b/packages/admin-sdk/src/params.ts@@ -334,9 +334,17 @@ export interface OrderUpdateParams {internal_note?: stringship_address?: AddressInputParamsbill_address?: AddressInputParams- line_items?: Array<{- variant_id?: string- quantity?: number+ /**+ * Line items to upsert, keyed by variant. An entry sets that variant's+ * quantity, creating the line item when the order has none; `quantity: 0`+ * removes it. Only the variants you send are touched — omitting one leaves+ * it alone — so a whole edited order can be submitted in a single request+ * and applied in one transaction.+ */+ items?: Array<{+ variant_id: string+ quantity: number+ metadata?: Record<string, unknown>}>}diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rbindex cab518bb3f..08cba77f6a 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb@@ -423,6 +423,28 @@ RSpec.describe Spree::Api::V3::Admin::OrdersController, type: :controller doexpect(json_response['email']).to eq('[email protected]')end+ # The admin order edit screen submits every row it shows in one request:+ # changed quantities as their new value, struck-out rows as zero.+ context 'with a whole edited order in one request' do+ let(:order) { create(:order_with_line_items, store: store, line_items_count: 2) }+ let(:kept) { order.line_items.first }+ let(:removed) { order.line_items.last }++ it 'applies the quantity changes and the removals together' do+ patch :update, params: {+ id: order.prefixed_id,+ items: [+ { variant_id: kept.variant.prefixed_id, quantity: 3 },+ { variant_id: removed.variant.prefixed_id, quantity: 0 }+ ]+ }, as: :json++ expect(response).to have_http_status(:ok)+ expect(order.reload.line_items).to contain_exactly(kept)+ expect(kept.reload.quantity).to eq(3)+ end+ end+context 'with customer assignment via customer_id' dolet(:customer) { create(:user) }diff --git a/spree/core/app/services/spree/orders/upsert_items.rb b/spree/core/app/services/spree/orders/upsert_items.rbindex 2677fa68b7..96a3f9ea57 100644--- a/spree/core/app/services/spree/orders/upsert_items.rb+++ b/spree/core/app/services/spree/orders/upsert_items.rb@@ -7,6 +7,11 @@ module Spree# For each entry in +items+:# - If a line item for the variant already exists -> sets its quantity# - If no line item exists -> creates one with the given quantity+ # - Quantity zero (or less) removes the line item when one exists, and is+ # ignored otherwise. That is what lets an admin submit a whole edited+ # order in one request: the rows the merchant struck out arrive as zeros+ # alongside the ones they changed, and the caller's transaction applies+ # the lot or none of it.## Order totals are NOT recalculated here. Callers (Spree::Orders::Create# and Spree::Orders::Update) are responsible for running shipment@@ -28,11 +33,18 @@ module Spreenext unless variantquantity = (item_params[:quantity] || 1).to_i- next if quantity <= 0+ line_item = Spree.line_item_by_variant_finder.new.execute(owner: order, variant: variant)- return failure(variant, "#{variant.name} is not available in #{order.currency}") if variant.amount_in(order.currency).nil?+ if quantity <= 0+ # Zero removes what is there and means nothing when there is+ # not, so an order arriving with its struck-out rows as zeros+ # settles in one pass while a create with a zero still adds+ # nothing.+ line_item&.destroy!+ next+ end- line_item = Spree.line_item_by_variant_finder.new.execute(owner: order, variant: variant)+ return failure(variant, "#{variant.name} is not available in #{order.currency}") if variant.amount_in(order.currency).nil?if line_itemline_item.quantity = quantitydiff --git a/spree/core/spec/services/spree/orders/upsert_items_spec.rb b/spree/core/spec/services/spree/orders/upsert_items_spec.rbindex 8f6b8e72dc..863535d556 100644--- a/spree/core/spec/services/spree/orders/upsert_items_spec.rb+++ b/spree/core/spec/services/spree/orders/upsert_items_spec.rb@@ -90,7 +90,7 @@ module Spreeendend- context 'with quantity zero' do+ context 'with quantity zero and nothing to remove' dolet(:items) { [{ variant_id: variant.prefixed_id, quantity: 0 }] }it 'skips the entry' do@@ -108,6 +108,48 @@ module Spreeendend+ # Zero is how an edited order says "this row is gone", so a whole order+ # can be submitted in one request rather than a delete per removed row.+ context 'with quantity zero on an existing line item' do+ let!(:existing_line_item) { create(:line_item, order: order, variant: variant, quantity: 5) }+ let(:items) { [{ variant_id: variant.prefixed_id, quantity: 0 }] }++ it 'removes the line item' do+ expect(subject).to be_success+ expect(order.line_items.reload).to be_empty+ end+ end++ context 'removing one line item while changing another' do+ let!(:kept) { create(:line_item, order: order, variant: variant2, quantity: 1) }+ let!(:removed) { create(:line_item, order: order, variant: variant, quantity: 5) }++ let(:items) do+ [+ { variant_id: variant.prefixed_id, quantity: 0 },+ { variant_id: variant2.prefixed_id, quantity: 4 }+ ]+ end++ it 'applies both in one pass' do+ expect(subject).to be_success+ expect(order.line_items.reload).to contain_exactly(kept)+ expect(kept.reload.quantity).to eq(4)+ end+ end++ # An upsert only touches what it is given — the screen sends every row,+ # so an omitted variant is not an instruction to delete it.+ context 'with a line item omitted from the payload' do+ let!(:untouched) { create(:line_item, order: order, variant: variant2, quantity: 2) }+ let(:items) { [{ variant_id: variant.prefixed_id, quantity: 1 }] }++ it 'leaves it alone' do+ expect(subject).to be_success+ expect(untouched.reload.quantity).to eq(2)+ end+ end+context 'with metadata on a new line item' dolet(:items) do[{ variant_id: variant.prefixed_id, quantity: 1, metadata: { 'gift' => true } }]