Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rescue from
InsufficientStock
error on frontend
checkout flow
When two users try to purchase the last item remaining from a non-backordeable stock location at the same time then the last one will experience an unhandled error `Spree::Order::InsufficientStock`. This happens only if there is a second backorderable stock location for the product. When there is no backorderable stock location the controller `before_action` `ensure_sufficient_stock_lines` is enough to catch the issue in advance. The error is generated by this line in Spree::Order model: `before_transition to: :complete, do: :validate_line_item_availability` Generally, `ensure_sufficient_stock_lines` prevent customers to complete the checkout process when there is not enough stock availability, but the case above is not caught here. So, by using `rescue_from` the customer is now redirected to the checkout `address` page and shown an error message that suggests to repeat the checkout process. The order's shipments will be rebuilt using the backorderable stock location in the delivery step, allowing them to (hopefully!) complete the purchase. `rescue_from` is already used on the `api` and `backend` section in order to manage `InsufficientStock` errors, so this was a natural choice also on the `frontend`.
- Loading branch information
1 parent
942ec5c
commit fb9a631
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
frontend/spec/features/checkout_confirm_insufficient_stock_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe "Checkout confirm page submission", type: :feature do | ||
include_context 'checkout setup' | ||
|
||
context "when the product from the order is not backorderable but has enough stock quantity" do | ||
let(:user) { create(:user) } | ||
|
||
let(:order) { Spree::TestingSupport::OrderWalkthrough.up_to(:payment) } | ||
let(:order_product) { order.products.first } | ||
let(:order_stock_item) { order_product.stock_items.first } | ||
|
||
before do | ||
order_stock_item.update! backorderable: false | ||
order_stock_item.set_count_on_hand(1) | ||
allow_any_instance_of(Spree::CheckoutController).to receive_messages(current_order: order) | ||
allow_any_instance_of(Spree::CheckoutController).to receive_messages(try_spree_current_user: user) | ||
allow_any_instance_of(Spree::OrdersController).to receive_messages(try_spree_current_user: user) | ||
end | ||
|
||
context 'when there are not other backorderable stock locations' do | ||
context 'when the customer is on the confirm page and the availabilty drops to zero' do | ||
before do | ||
visit spree.checkout_state_path(:confirm) | ||
order_stock_item.set_count_on_hand(0) | ||
end | ||
|
||
it 'redirects to cart page and shows an unavailable product message' do | ||
click_button "Place Order" | ||
expect(page).to have_content "#{order_product.name} became unavailable" | ||
expect(page).to have_current_path spree.cart_path | ||
end | ||
end | ||
end | ||
|
||
context 'when there is another backorderable stock location' do | ||
before do | ||
create :stock_location, backorderable_default: true, default: false | ||
end | ||
|
||
context 'when the customer is on the confirm page and the availabilty drops to zero' do | ||
before do | ||
visit spree.checkout_state_path(:confirm) | ||
order_stock_item.set_count_on_hand(0) | ||
end | ||
|
||
it "redirects to the address checkout page and shows an availability changed message" do | ||
click_button "Place Order" | ||
error_message = "Quantity selected of #{order_product.name} is not available. Still, items may be available from another stock location, please try again." | ||
expect(page).to have_content error_message | ||
expect(page).to have_current_path spree.checkout_state_path(:address) | ||
end | ||
|
||
it "can still complete the order using the backorderable stock location by restarting the checkout" do | ||
click_button "Place Order" | ||
expect(page).to have_current_path spree.checkout_state_path(:address) | ||
click_button "Save and Continue" | ||
expect(page).to have_current_path spree.checkout_state_path(:delivery) | ||
click_button "Save and Continue" | ||
expect(page).to have_current_path spree.checkout_state_path(:payment) | ||
click_button "Save and Continue" | ||
expect(page).to have_current_path spree.checkout_state_path(:confirm) | ||
click_button "Place Order" | ||
expect(page).to have_content 'Your order has been processed successfully' | ||
end | ||
end | ||
end | ||
end | ||
end |