Display out of stock banner when viewing cart directly

This commit is contained in:
Rohan Mitchell
2016-04-14 11:14:16 +10:00
parent 9b3139dba9
commit 3dcfa810fd
4 changed files with 30 additions and 7 deletions

View File

@@ -153,12 +153,10 @@ class CheckoutController < Spree::CheckoutController
def raise_insufficient_quantity
respond_to do |format|
format.html do
flash[:error] = t(:spree_inventory_error_flash_for_insufficient_quantity)
redirect_to cart_path
end
format.json do
flash[:error] = t(:spree_inventory_error_flash_for_insufficient_quantity)
render json: {path: cart_path}, status: 400
end
end

View File

@@ -32,7 +32,6 @@ class EnterprisesController < BaseController
def check_stock_levels
if current_order(true).insufficient_stock_lines.present?
flash[:error] = t(:spree_inventory_error_flash_for_insufficient_quantity)
redirect_to spree.cart_path
end
end

View File

@@ -1,9 +1,9 @@
require 'spree/core/controller_helpers/order_decorator'
Spree::OrdersController.class_eval do
after_filter :populate_variant_attributes, :only => :populate
before_filter :update_distribution, :only => :update
before_filter :filter_order_params, :only => :update
after_filter :populate_variant_attributes, only: :populate
before_filter :update_distribution, only: :update
before_filter :filter_order_params, only: :update
prepend_before_filter :require_order_cycle, only: :edit
prepend_before_filter :require_distributor_chosen, only: :edit
@@ -12,13 +12,19 @@ Spree::OrdersController.class_eval do
include OrderCyclesHelper
layout 'darkswarm'
# Patching to redirect to shop if order is empty
def edit
@order = current_order(true)
if @order.line_items.empty?
redirect_to main_app.shop_path
else
associate_user
if @order.insufficient_stock_lines.present?
flash[:error] = t(:spree_inventory_error_flash_for_insufficient_quantity)
end
end
end

View File

@@ -42,6 +42,26 @@ describe Spree::OrdersController do
flash[:info].should == "The hub you have selected is temporarily closed for orders. Please try again later."
end
describe "when an item has insufficient stock" do
let(:order) { subject.current_order(true) }
let(:oc) { create(:simple_order_cycle, distributors: [d], variants: [variant]) }
let(:d) { create(:distributor_enterprise, shipping_methods: [create(:shipping_method)], payment_methods: [create(:payment_method)]) }
let(:variant) { create(:variant, on_demand: false, on_hand: 5) }
let(:line_item) { order.line_items.last }
before do
order.set_distribution! d, oc
order.add_variant variant, 5
variant.update_attributes! on_hand: 3
end
it "displays a flash message when we view the cart" do
spree_get :edit
expect(response.status).to eq 200
flash[:error].should == "An item in your cart has become unavailable."
end
end
describe "returning stock levels in JSON on success" do
let(:product) { create(:simple_product) }
@@ -120,7 +140,7 @@ describe Spree::OrdersController do
describe "when I pass params that includes a line item no longer in our cart" do
it "should silently ignore the missing line item" do
order = subject.current_order(true)
li = order.add_variant(create(:simple_product, on_hand: 110).master)
li = order.add_variant(create(:simple_product, on_hand: 110).variants.first)
spree_get :update, order: { line_items_attributes: {
"0" => {quantity: "0", id: "9999"},
"1" => {quantity: "99", id: li.id}