When cart is updated with insufficient stock, show amount in cart, not amount entered

This commit is contained in:
Rohan Mitchell
2016-04-27 12:22:51 +10:00
parent 8f8a1191cb
commit cf40e0432a
3 changed files with 73 additions and 16 deletions

View File

@@ -16,6 +16,7 @@ Spree::OrdersController.class_eval do
# Patching to redirect to shop if order is empty
def edit
@order = current_order(true)
@insufficient_stock_lines = @order.insufficient_stock_lines
if @order.line_items.empty?
redirect_to main_app.shop_path
@@ -28,6 +29,41 @@ Spree::OrdersController.class_eval do
end
end
def update
@insufficient_stock_lines = []
@order = current_order
unless @order
flash[:error] = t(:order_not_found)
redirect_to root_path and return
end
if @order.update_attributes(params[:order])
@order.line_items = @order.line_items.select {|li| li.quantity > 0 }
@order.restart_checkout_flow
render :edit and return unless apply_coupon_code
fire_event('spree.order.contents_changed')
respond_with(@order) do |format|
format.html do
if params.has_key?(:checkout)
@order.next_transition.run_callbacks if @order.cart?
redirect_to checkout_state_path(@order.checkout_steps.first)
else
redirect_to cart_path
end
end
end
else
# Show order with original values, not newly entered ones
@insufficient_stock_lines = @order.insufficient_stock_lines
@order.line_items(true)
respond_with(@order)
end
end
def populate
# Without intervention, the Spree::Adjustment#update_adjustable callback is called many times
# during cart population, for both taxation and enterprise fees. This operation triggers a
@@ -55,6 +91,7 @@ Spree::OrdersController.class_eval do
end
end
# Report the stock levels in the order for all variant ids requested
def stock_levels(order, variant_ids)
stock_levels = li_stock_levels(order)

View File

@@ -17,7 +17,7 @@
= render 'spree/shared/line_item_name', line_item: line_item
- if @order.insufficient_stock_lines.include? line_item
- if @insufficient_stock_lines.include? line_item
%span.out-of-stock
= variant.in_stock? ? t(:insufficient_stock, :on_hand => variant.on_hand) : t(:out_of_stock)
%br/

View File

@@ -7,25 +7,45 @@ feature "full-page cart", js: true do
include UIComponentHelper
describe "viewing the cart" do
let!(:zone) { create(:zone_with_member) }
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true, charges_sales_tax: true) }
let(:supplier) { create(:supplier_enterprise) }
let!(:order_cycle) { create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], coordinator: create(:distributor_enterprise), variants: [product.variants.first]) }
let(:enterprise_fee) { create(:enterprise_fee, amount: 11.00, tax_category: product.tax_category) }
let(:product) { create(:taxed_product, supplier: supplier, zone: zone, price: 110.00, tax_rate_amount: 0.1) }
let(:variant) { product.variants.first }
let(:order) { create(:order, order_cycle: order_cycle, distributor: distributor) }
before do
add_enterprise_fee enterprise_fee
set_order order
add_product_to_cart
visit spree.cart_path
end
describe "tax" do
let!(:zone) { create(:zone_with_member) }
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true, charges_sales_tax: true) }
let(:supplier) { create(:supplier_enterprise) }
let!(:order_cycle) { create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], coordinator: create(:distributor_enterprise), variants: [product.variants.first]) }
let(:enterprise_fee) { create(:enterprise_fee, amount: 11.00, tax_category: product.tax_category) }
let(:product) { create(:taxed_product, supplier: supplier, zone: zone, price: 110.00, tax_rate_amount: 0.1) }
let(:order) { create(:order, order_cycle: order_cycle, distributor: distributor) }
before do
add_enterprise_fee enterprise_fee
set_order order
add_product_to_cart
visit spree.cart_path
end
it "shows the total tax for the order, including product tax and tax on fees" do
page.should have_selector '.tax-total', text: '11.00' # 10 + 1
end
end
describe "updating quantities with insufficient stock available" do
let(:li) { order.line_items(true).last }
use_short_wait 10
before do
variant.update_attributes! on_hand: 2
end
it "shows the quantities saved, not those submitted" do
fill_in "order_line_items_attributes_0_quantity", with: '4'
click_button 'Update'
page.should have_field "order[line_items_attributes][0][quantity]", with: '1'
page.should have_content "Insufficient stock available, only 2 remaining"
end
end
end
end