Redirect user to cart page if some item in the order is unavailable

This commit is contained in:
luisramos0
2019-04-13 17:43:28 +01:00
parent 59ec52babe
commit d602375ac7
4 changed files with 47 additions and 18 deletions

View File

@@ -169,7 +169,7 @@ class CheckoutController < Spree::CheckoutController
def load_order
@order = current_order
redirect_to main_app.shop_path and return unless @order and @order.checkout_allowed?
raise_insufficient_quantity and return if @order.insufficient_stock_lines.present?
redirect_to_cart_path and return unless valid_order_line_items?
redirect_to main_app.shop_path and return if @order.completed?
before_address
setup_for_current_state
@@ -184,8 +184,11 @@ class CheckoutController < Spree::CheckoutController
@order.ship_address = finder.ship_address
end
# Overriding Spree's methods
def raise_insufficient_quantity
def valid_order_line_items?
@order.insufficient_stock_lines.empty? && OrderCycleDistributedVariants.new(@order.order_cycle, @order.distributor).distributes_order_variants?(@order)
end
def redirect_to_cart_path
respond_to do |format|
format.html do
redirect_to cart_path

View File

@@ -36,22 +36,40 @@ describe CheckoutController, type: :controller do
flash[:info].should == "The hub you have selected is temporarily closed for orders. Please try again later."
end
it "redirects to the cart when some items are out of stock" do
controller.stub(:current_distributor).and_return(distributor)
controller.stub(:current_order_cycle).and_return(order_cycle)
controller.stub(:current_order).and_return(order)
order.stub_chain(:insufficient_stock_lines, :present?).and_return true
get :edit
response.should redirect_to spree.cart_path
end
describe "redirection to the cart" do
let(:order_cycle_distributed_variants) { double(:order_cycle_distributed_variants) }
it "renders when both distributor and order cycle is selected" do
controller.stub(:current_distributor).and_return(distributor)
controller.stub(:current_order_cycle).and_return(order_cycle)
controller.stub(:current_order).and_return(order)
order.stub_chain(:insufficient_stock_lines, :present?).and_return false
get :edit
response.should be_success
before do
controller.stub(:current_order).and_return(order)
order.stub(:distributor).and_return(distributor)
order.order_cycle = order_cycle
allow(OrderCycleDistributedVariants).to receive(:new).with(order_cycle, distributor).and_return(order_cycle_distributed_variants)
end
it "redirects when some items are out of stock" do
allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return false
get :edit
expect(response).to redirect_to spree.cart_path
end
it "redirects when some items are not available" do
allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return true
expect(order_cycle_distributed_variants).to receive(:distributes_order_variants?).with(order).and_return(false)
get :edit
expect(response).to redirect_to spree.cart_path
end
it "does not redirect when items are available and in stock" do
allow(order).to receive_message_chain(:insufficient_stock_lines, :empty?).and_return true
expect(order_cycle_distributed_variants).to receive(:distributes_order_variants?).with(order).and_return(true)
get :edit
expect(response).to be_success
end
end
describe "building the order" do

View File

@@ -23,6 +23,10 @@ describe "checking out an order that initially fails", type: :request do
end
before do
order_cycle_distributed_variants = double(:order_cycle_distributed_variants)
allow(OrderCycleDistributedVariants).to receive(:new).and_return(order_cycle_distributed_variants)
allow(order_cycle_distributed_variants).to receive(:distributes_order_variants?).and_return(true)
order.reload.update_totals
set_order order
end

View File

@@ -27,6 +27,10 @@ describe "checking out an order with a Stripe Connect payment method", type: :re
end
before do
order_cycle_distributed_variants = double(:order_cycle_distributed_variants)
allow(OrderCycleDistributedVariants).to receive(:new).and_return(order_cycle_distributed_variants)
allow(order_cycle_distributed_variants).to receive(:distributes_order_variants?).and_return(true)
allow(Stripe).to receive(:api_key) { "sk_test_12345" }
order.update_attributes(distributor_id: enterprise.id, order_cycle_id: order_cycle.id)
order.reload.update_totals