Add basic out of stock modal

This commit is contained in:
Gaetan Craig-Riou
2025-01-27 20:52:41 +11:00
parent a7cde069c6
commit c73c2dd8a8
4 changed files with 17 additions and 32 deletions

View File

@@ -11,11 +11,12 @@ module OrderStockCheck
end
def handle_insufficient_stock
stock_service = Orders::CheckStockService.new(@order)
@any_out_of_stock = false
stock_service = Orders::CheckStockService.new(order: @order)
return if stock_service.sufficient_stock?
flash[:error] = Spree.t(:inventory_error_flash_for_insufficient_quantity)
redirect_to main_app.cart_path
@any_out_of_stock = true
end
def check_order_cycle_expiry

View File

@@ -20,6 +20,11 @@
= render partial: "shopping_shared/order_cycles"
%div{ "data-controller": "guest-checkout", "data-guest-checkout-distributor-value": @order.distributor.id }
= #TODO create new component ? reuse out_of_stock.html.haml template
- if @any_out_of_stock
= render ModalComponent.new(id: "out-of-stock-items", instant: true) do
%div
Some items are out of stock
%div{ style: "display: #{spree_current_user ? 'block' : 'none'}", "data-guest-checkout-target": "checkout" }
= render partial: "checkout"

View File

@@ -66,12 +66,4 @@ module CheckoutHelper
click_on "Complete order"
expect(page).to have_content "Back To Store"
end
def out_of_stock_check(step)
visit checkout_step_path(step)
expect(page).not_to have_selector 'closing', text: "Checkout now"
expect(page).to have_selector 'closing', text: "Your shopping cart"
expect(page).to have_content "An item in your cart has become unavailable"
end
end

View File

@@ -88,21 +88,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do
end
end
shared_examples "when I have an out of stock product in my cart" do
before do
variant.update!(on_demand: false, on_hand: 0)
end
it "returns me to the cart with an error message" do
visit checkout_path
expect(page).not_to have_selector 'closing', text: "Checkout now"
expect(page).to have_selector 'closing', text: "Your shopping cart"
expect(page).to have_content "An item in your cart has become unavailable"
expect(page).to have_content "Update"
end
end
context "as a guest user" do
before do
visit checkout_path
@@ -222,8 +207,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do
"Local", "Shipping with Fee", "Z Free Shipping without required address"
]
end
it_behaves_like "when I have an out of stock product in my cart"
end
context "on the 'payment' step" do
@@ -235,8 +218,6 @@ RSpec.describe "As a consumer, I want to checkout my order" do
it "should allow visit '/checkout/payment'" do
expect(page).to have_current_path("/checkout/payment")
end
it_behaves_like "when I have an out of stock product in my cart"
end
describe "hidding a shipping method" do
@@ -305,8 +286,14 @@ RSpec.describe "As a consumer, I want to checkout my order" do
login_as(user)
end
end
it "returns me to the cart with an error message" do
out_of_stock_check(step)
it "displays a modal warning the user of updated cart" do
visit checkout_step_path(step)
expect(page).to have_selector 'closing', text: "Checkout now"
within "#out-of-stock-items" do
expect(page).to have_content "Some items are out of stock"
end
end
end
end