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.

Fixed Checkout `remove_missing_items` action

Damian Legawiec5920ff7ae3e8
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 757c3cc1fb..e60fb2f148 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -137,6 +137,8 @@ module Spree
end
has_many :shipment_adjustments, through: :shipments, source: :adjustments
+ attribute :invalid_line_items, array: true, default: []
+
accepts_nested_attributes_for :line_items
accepts_nested_attributes_for :bill_address
accepts_nested_attributes_for :ship_address
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index f4b8eea705..8e26f70172 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -1978,4 +1978,12 @@ describe Spree::Order, type: :model do
expect(subject).to eq([variant])
end
end
+
+ describe '#remove_invalid_line_items' do
+ let(:order) { create(:order_with_line_items, store: store) }
+
+ it 'removes invalid line items' do
+ expect { order.remove_invalid_line_items(order.line_items.pluck(:id)) }.to change { order.line_items.count }.from(1).to(0)
+ end
+ end
end
diff --git a/storefront/app/controllers/spree/checkout_controller.rb b/storefront/app/controllers/spree/checkout_controller.rb
index 6a7f255f58..046d23ea60 100644
--- a/storefront/app/controllers/spree/checkout_controller.rb
+++ b/storefront/app/controllers/spree/checkout_controller.rb
@@ -77,9 +77,15 @@ module Spree
# DELETE /checkout/<token>/remove_missing_items
def remove_missing_items
- @order.remove_invalid_line_items(params[:line_item_ids])
- @order.next
- redirect_to spree.checkout_state_path(@order.token, @order.state)
+ line_items_to_remove = @order.line_items.where(id: params[:line_item_ids])
+
+ ApplicationRecord.transaction do
+ line_items_to_remove.each do |line_item|
+ remove_line_item_service.call(order: @order, line_item: line_item)
+ end
+ end
+
+ redirect_to spree.checkout_path(@order.token)
end
# GET /checkout/<token>/complete
@@ -344,6 +350,10 @@ module Spree
Spree::Dependencies.checkout_remove_store_credit_service.constantize
end
+ def remove_line_item_service
+ Spree::Dependencies.cart_remove_line_item_service.constantize
+ end
+
def coupon_handler
Spree::Dependencies.coupon_handler.constantize
end
diff --git a/storefront/app/views/spree/checkout/_missing_line_items.html.erb b/storefront/app/views/spree/checkout/_missing_line_items.html.erb
index 4da1095434..8c7117555f 100644
--- a/storefront/app/views/spree/checkout/_missing_line_items.html.erb
+++ b/storefront/app/views/spree/checkout/_missing_line_items.html.erb
@@ -28,7 +28,7 @@
<div class="flex justify-end items-center flex-wrap mt-6">
<%= link_to 'return to cart', spree.cart_path, class: 'mx-4 py-4', target: :_top %>
<%= button_to 'Continue to checkout',
- spree.checkout_remove_missing_items_path(@order.token, @order.state, line_item_ids: @order.invalid_line_items.map(&:id)),
+ spree.checkout_remove_missing_items_path(@order.token, line_item_ids: @order.invalid_line_items.map(&:id)),
class: 'btn-primary font-semibold checkout-content-save-continue-button my-5 ml-4 !py-4',
method: :delete,
data: { turbo: false, 'turbo-submits-with': (render 'spree/checkout/button_processing', is_hidden: false) }
diff --git a/storefront/app/views/spree/checkout/edit.html.erb b/storefront/app/views/spree/checkout/edit.html.erb
index 889f977f73..a88bf6fd07 100644
--- a/storefront/app/views/spree/checkout/edit.html.erb
+++ b/storefront/app/views/spree/checkout/edit.html.erb
@@ -51,7 +51,7 @@
<div class="mt-3 border-t pt-3 flex justify-between items-center border-default">
<div class="flex items-center">
<div class="min-w-[60px] max-w-[60px]">
- <%= Spree.t(:delivery_method) %>
+ <%= Spree.t('storefront.checkout.delivery_method') %>
</div>
<div class="px-5 flex flex-col gap-4">
<% @order.shipments.each do |shipment| %>
diff --git a/storefront/config/routes.rb b/storefront/config/routes.rb
index b46f7022ca..61453548bb 100644
--- a/storefront/config/routes.rb
+++ b/storefront/config/routes.rb
@@ -48,7 +48,7 @@ Spree::Core::Engine.add_routes do
get '/checkout/:token/:state', to: 'checkout#edit', as: :checkout_state
patch '/checkout/:token/update/:state', to: 'checkout#update', as: :update_checkout
get '/checkout/:token', to: 'checkout#edit', as: :checkout
- delete '/checkout/:token/remove_missing_items/:state', to: 'checkout#remove_missing_items', as: :checkout_remove_missing_items
+ delete '/checkout/:token/remove_missing_items', to: 'checkout#remove_missing_items', as: :checkout_remove_missing_items
# Account
resources :addresses, except: [:index]
diff --git a/storefront/spec/controllers/spree/checkout_controller_spec.rb b/storefront/spec/controllers/spree/checkout_controller_spec.rb
index acfb85bac7..b22b8a4ade 100644
--- a/storefront/spec/controllers/spree/checkout_controller_spec.rb
+++ b/storefront/spec/controllers/spree/checkout_controller_spec.rb
@@ -17,6 +17,7 @@ describe Spree::CheckoutController, type: :controller do
allow(controller).to receive(:current_store).and_return(store)
allow(controller).to receive_messages try_spree_current_user: user
allow(controller).to receive_messages spree_current_user: user
+ allow(controller).to receive(:spree_login_path).and_return('/login')
end
describe '#edit' do
@@ -1064,6 +1065,17 @@ describe Spree::CheckoutController, type: :controller do
end
end
+ describe '#remove_missing_items' do
+ let(:order) { create(:order_with_line_items, store: store, user: user, line_items_count: 3) }
+ let(:line_item) { order.line_items.first }
+ let(:line_item_2) { order.line_items.last }
+
+ it 'removes missing items' do
+ expect { delete :remove_missing_items, params: { token: order.token, line_item_ids: [line_item.id, line_item_2.id] } }.to change { order.line_items.count }.from(3).to(1)
+ expect(response).to redirect_to spree.checkout_path(order.token)
+ end
+ end
+
describe '#load_order' do
subject do
get :edit, params: { state: 'address', token: order.token }