From f3ffba378a76113e778c77749c6a1a2a73ef3417 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 3 Aug 2012 18:05:45 +1000 Subject: [PATCH] Add a product with variants to the cart with max quantity for a group buy --- .../spree/orders_controller_decorator.rb | 10 +++++++--- spec/requests/consumer/add_to_cart_spec.rb | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 0dbc072a92..438f008ce6 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -16,14 +16,18 @@ Spree::OrdersController.class_eval do end def populate_variant_attributes - puts "params: #{params.inspect}" - puts "Has key variant_attributes? #{params.key?(:variant_attributes)}" if params.key? :variant_attributes params[:variant_attributes].each do |variant_id, attributes| - puts "Setting variant attributes for variant #{variant_id}, attributes: #{attributes.inspect}" @order.set_variant_attributes(Spree::Variant.find(variant_id), attributes) end end + + if params.key? :quantity + params[:products].each do |product_id, variant_id| + max_quantity = params[:max_quantity].to_i + @order.set_variant_attributes(Spree::Variant.find(variant_id), {:max_quantity => max_quantity}) + end + end end diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index 1610859e54..afb4440a8a 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -125,6 +125,26 @@ feature %q{ li.max_quantity.should == 3 end + scenario "adding a product with variants to the cart for a group buy" do + # Given a group buy product with variants and a distributor + d = create(:distributor) + p = create(:product, :distributors => [d], :group_buy => true) + create(:variant, :product => p) + + # When I add the item to my cart + visit spree.product_path p + fill_in "quantity", :with => 2 + fill_in "max_quantity", :with => 3 + click_button 'Add To Cart' + + # Then the item should be in my cart with correct quantities + order = Spree::Order.last + li = order.line_items.first + li.product.should == p + li.quantity.should == 2 + li.max_quantity.should == 3 + end + scenario "adding a product to cart that is not a group buy does not show max quantity field" do # Given a group buy product and a distributor d = create(:distributor)