Ajax submission of max quantity

This commit is contained in:
Will Marshall
2014-07-28 13:33:13 +10:00
parent 39f9e991ad
commit a5f478bde7
7 changed files with 62 additions and 16 deletions

View File

@@ -24,7 +24,9 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)->
data: =>
variants = {}
for li in @line_items_present()
variants[li.variant.id] = li.quantity
variants[li.variant.id] =
quantity: li.quantity
max_quantity: li.max_quantity
{variants: variants}
@@ -56,4 +58,5 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)->
variant.line_item =
variant: variant
quantity: 0
max_quantity: null
@line_items.push variant.line_item

View File

@@ -92,6 +92,30 @@ Spree::Order.class_eval do
end
end
# Overridden to support max_quantity
def add_variant(variant, quantity = 1, max_quantity = nil, currency = nil)
current_item = find_line_item_by_variant(variant)
if current_item
current_item.quantity += quantity
current_item.max_quantity += max_quantity.to_i
current_item.currency = currency unless currency.nil?
current_item.save
else
current_item = Spree::LineItem.new(:quantity => quantity, max_quantity: max_quantity)
current_item.variant = variant
if currency
current_item.currency = currency unless currency.nil?
current_item.price = variant.price_in(currency).amount
else
current_item.price = variant.price
end
self.line_items << current_item
end
self.reload
current_item
end
def set_distributor!(distributor)
self.distributor = distributor
self.order_cycle = nil unless self.order_cycle.andand.has_distributor? distributor

View File

@@ -10,12 +10,13 @@ Spree::OrderPopulator.class_eval do
if valid?
@order.with_lock do
@order.empty! if overwrite
from_hash[:products].each do |product_id,variant_id|
from_hash[:products].each do |product_id, variant_id|
attempt_cart_add(variant_id, from_hash[:quantity])
end if from_hash[:products]
from_hash[:variants].each do |variant_id, quantity|
attempt_cart_add(variant_id, quantity)
from_hash[:variants].each do |variant_id, args|
attempt_cart_add(variant_id, args[:quantity], args[:max_quantity])
end if from_hash[:variants]
end
end
@@ -23,16 +24,14 @@ Spree::OrderPopulator.class_eval do
valid?
end
# Copied from Spree::OrderPopulator, with additional validations added
def attempt_cart_add(variant_id, quantity)
def attempt_cart_add(variant_id, quantity, max_quantity = nil)
quantity = quantity.to_i
variant = Spree::Variant.find(variant_id)
if quantity > 0
if check_stock_levels(variant, quantity) &&
check_order_cycle_provided_for(variant) &&
check_variant_available_under_distribution(variant)
@order.add_variant(variant, quantity, currency)
@order.add_variant(variant, quantity, max_quantity, currency)
end
end
end

View File

@@ -168,9 +168,12 @@ feature "As a consumer I want to shop with a distributor", js: true do
it "should save group buy data to ze cart" do
fill_in "variants[#{variant.id}]", with: 6
fill_in "variant_attributes[#{variant.id}][max_quantity]", with: 7
add_to_cart
page.should have_content product.name
page.should have_in_cart product.name
li = Spree::Order.order(:created_at).last.line_items.order(:created_at).last
while li == nil
sleep 0.1
li = Spree::Order.order(:created_at).last.line_items.order(:created_at).last
end
li.max_quantity.should == 7
li.quantity.should == 6
end
@@ -188,9 +191,7 @@ feature "As a consumer I want to shop with a distributor", js: true do
end
it "should let us add products to our cart" do
fill_in "variants[#{variant.id}]", with: "1"
add_to_cart
current_path.should == "/cart"
page.should have_content product.name
page.should have_in_cart product.name
end
end

View File

@@ -34,6 +34,15 @@ module Spree
order.should_receive(:with_lock)
op.populate(params, true)
end
it "attempts cart add with max_quantity" do
op.stub(:distribution_can_supply_products_in_cart).and_return true
order.should_receive(:empty!)
params = {variants: {"1" => {quantity: 1, max_quantity: 2}}}
order.stub(:with_lock).and_yield
op.should_receive(:attempt_cart_add).with("1", 1, 2).and_return true
op.populate(params, true)
end
end
describe "attempt_cart_add" do
@@ -46,7 +55,7 @@ module Spree
op.should_receive(:check_order_cycle_provided_for).with(variant).and_return(true)
op.should_receive(:check_variant_available_under_distribution).with(variant).
and_return(true)
order.should_receive(:add_variant).with(variant, quantity, currency)
order.should_receive(:add_variant).with(variant, quantity, nil, currency)
op.attempt_cart_add(333, quantity.to_s)
end

View File

@@ -9,8 +9,7 @@ describe Spree::Order do
subject.distributor = d
subject.save!
subject.add_variant(p.master, 1)
subject.set_variant_attributes(p.master, {'max_quantity' => '3'})
subject.add_variant(p.master, 1, 3)
li = Spree::LineItem.last
li.max_quantity.should == 3

View File

@@ -56,6 +56,17 @@ module UIComponentHelper
have_content "An email with instructions on resetting your password has been sent!"
end
def have_in_cart name
show_cart
within "li.cart" do
have_content name
end
end
def show_cart
find("#cart").click
end
def be_logged_in_as(user_or_email)
if user_or_email.is_a? Spree::User
have_content user_or_email.email