diff --git a/app/services/order_factory.rb b/app/services/order_factory.rb index fc4bd5f424..19c579e15b 100644 --- a/app/services/order_factory.rb +++ b/app/services/order_factory.rb @@ -48,7 +48,7 @@ class OrderFactory attrs[:line_items].each do |li| next unless variant = Spree::Variant.find_by_id(li[:variant_id]) scoper.scope(variant) - li[:quantity] = stock_limited_quantity(variant.on_hand, li[:quantity]) + li[:quantity] = stock_limited_quantity(variant.on_demand, variant.on_hand, li[:quantity]) li[:price] = variant.price build_item_from(li) end @@ -81,9 +81,9 @@ class OrderFactory @order.payments.create(payment_method_id: attrs[:payment_method_id], amount: @order.reload.total) end - def stock_limited_quantity(stock, requested) - return requested if opts[:skip_stock_check] - [stock, requested].min + def stock_limited_quantity(variant_on_demand, variant_on_hand, requested) + return requested if opts[:skip_stock_check] || variant_on_demand + [variant_on_hand, requested].min end def scoper diff --git a/spec/services/order_factory_spec.rb b/spec/services/order_factory_spec.rb index 7c9c0bd7d5..0c250b786e 100644 --- a/spec/services/order_factory_spec.rb +++ b/spec/services/order_factory_spec.rb @@ -31,7 +31,7 @@ describe OrderFactory do attrs end - it "builds a new order based the provided attributes" do + it "builds a new order based on the provided attributes" do expect{ order }.to change{ Spree::Order.count }.by(1) expect(order).to be_a Spree::Order expect(order.line_items.count).to eq 2 @@ -78,11 +78,21 @@ describe OrderFactory do end context "when skip_stock_check is not requested" do - it "initialised the order but limits stock to the available amount" do + it "initialises the order but limits stock to the available amount" do expect{ order }.to change{ Spree::Order.count }.by(1) expect(order).to be_a Spree::Order expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 2 end + + context "when variant is on_demand" do + before { variant1.update_attribute(:on_demand, true) } + + it "initialises the order with the requested quantity regardless of stock" do + expect{ order }.to change{ Spree::Order.count }.by(1) + expect(order).to be_a Spree::Order + expect(order.line_items.find_by_variant_id(variant1.id).quantity).to eq 5 + end + end end context "when skip_stock_check is requested" do