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 i18n ordering and owner-aware line item removal
Damian Legawiecc84dcd7c2e7f
diff --git a/spree/core/app/jobs/spree/variants/remove_line_item_job.rb b/spree/core/app/jobs/spree/variants/remove_line_item_job.rbindex b7b8e4417b..c8c9addcda 100644--- a/spree/core/app/jobs/spree/variants/remove_line_item_job.rb+++ b/spree/core/app/jobs/spree/variants/remove_line_item_job.rb@@ -4,16 +4,24 @@ module Spreequeue_as Spree.queues.variantsdef perform(line_item:)- workflow = Spree.cart_upsert_items_workflow.new+ owner = line_item.owner+ return if owner.nil?++ # Carts and draft orders have different contracts: the cart side turns+ # a vetoed removal into a warning and still succeeds, the order side+ # fails outright. Routing an order-owned line through the cart+ # workflow would apply cart semantics to an admin edit.+ cart_owned = owner.is_a?(Spree::Cart)+ workflow = (cart_owned ? Spree.cart_upsert_items_workflow : Spree.order_upsert_items_workflow).new+result = workflow.call(- cart: line_item.owner,+ **(cart_owned ? { cart: owner } : { order: owner }),items: [{ variant_id: line_item.variant_id, quantity: 0 }])- # The cart-side contract turns a vetoed removal into a warning and- # still succeeds, so the job has to look at the warnings to know the- # line survived — otherwise a discontinued variant silently stays in- # the cart with nothing to show for it.+ # A cart-side veto surfaces as a warning rather than a failed result,+ # so both have to be checked — otherwise a discontinued variant stays+ # in the cart with nothing to show for it.rejection = workflow.warnings.firstreturn if result.success? && rejection.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 d4d3d4ec4e..593eabee1b 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@@ -15,6 +15,8 @@ module Spreeline_items = cart.line_items.includes(variant: [:product, :stock_locations, { stock_items: [:stock_location, :active_stock_reservations] }])+ removal_error = nil+ActiveRecord::Base.transaction do# The predicates record a message per candidate as they run, so the# messages exist before the removal is attempted.@@ -22,11 +24,20 @@ module Spreeif removals.any?workflow = Spree.cart_upsert_items_workflow.new- workflow.call(+ result = workflow.call(cart: cart,items: removals.map { |line_item| { variant_id: line_item.variant_id, quantity: 0 } })+ # Nothing was removed — reporting the candidates as removed would+ # tell the customer their cart changed when it did not. Raise+ # rather than return so the transaction rolls back; a bare return+ # would commit it.+ if result.failure?+ removal_error = result.error+ raise ActiveRecord::Rollback+ end+# A :validate handler can veto a removal, leaving the line in the# cart. Telling the customer it was removed would be a lie, so# retract the message for anything that survived.@@ -34,6 +45,8 @@ module Spreeendend+ return failure(cart, removal_error) if removal_error+if @messages.any? # If any line item was removed, reload the cartsuccess([cart.reload, @messages, @warnings])elsediff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex 8d656796df..8cece6bc0f 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -678,10 +678,10 @@ en:cart_already_updated: The cart has already been updated.cart_line_item:currency_unavailable: "%{li_name} is not available in %{currency}"- quantity_must_be_positive: Quantity must be at least 1. Use DELETE to remove the item.delivery_unavailable: "%{li_name} cannot be delivered to the selected address"discontinued: "%{li_name} was removed because it was discontinued"out_of_stock: "%{li_name} was removed because it was sold out"+ quantity_must_be_positive: Quantity must be at least 1. Use DELETE to remove the item.categories: Categoriescategory: Categorychange: Changediff --git a/spree/core/spec/jobs/spree/variants/remove_line_item_job_spec.rb b/spree/core/spec/jobs/spree/variants/remove_line_item_job_spec.rbindex 95218a1ca6..fea087127b 100644--- a/spree/core/spec/jobs/spree/variants/remove_line_item_job_spec.rb+++ b/spree/core/spec/jobs/spree/variants/remove_line_item_job_spec.rb@@ -10,5 +10,46 @@ module Spreedescribed_class.perform_now(line_item: line_item)}.to change { order.reload.line_items.count }.by(-1)end++ # Carts and draft orders have different validation contracts, so the job+ # has to pick the workflow that matches the line item's owner.+ it 'routes an order-owned line item through the order workflow' do+ expect(Spree::Orders::UpsertItems).to receive(:new).and_call_original+ expect(Spree::Carts::UpsertItems).not_to receive(:new)++ described_class.perform_now(line_item: line_item)+ end++ context 'with a cart-owned line item' do+ let!(:cart) { create(:cart, store: @default_store) }+ let!(:cart_line_item) { create(:line_item, cart: cart, order: nil) }++ it 'removes the line item from the cart' do+ expect {+ described_class.perform_now(line_item: cart_line_item)+ }.to change { cart.reload.line_items.count }.by(-1)+ end++ it 'routes through the cart workflow' do+ expect(Spree::Carts::UpsertItems).to receive(:new).and_call_original++ described_class.perform_now(line_item: cart_line_item)+ end++ # A vetoed removal succeeds on the cart side as a warning, so the job+ # would otherwise finish silently with the line still there.+ it 'reports a vetoed removal instead of finishing quietly' do+ Spree.hooks.register('carts.upsert_items.validate') { |flow| flow.reject!('keep it') }+ expect(Rails.error).to receive(:report).with(+ an_instance_of(described_class::RemovalRejected), hash_including(source: 'spree.variants.remove_line_item')+ )++ expect {+ described_class.perform_now(line_item: cart_line_item)+ }.not_to change { cart.reload.line_items.count }+ ensure+ Spree.hooks.clear!+ end+ endendend