mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
With order cycle distribution, show error message when adding a product to cart with no distribution chosen
This commit is contained in:
@@ -28,13 +28,19 @@ Spree::OrdersController.class_eval do
|
||||
|
||||
def populate_order_distributor
|
||||
@distributor = params[:distributor_id].present? ? Enterprise.is_distributor.find(params[:distributor_id]) : nil
|
||||
@order_cycle = params[:order_cycle_id].present? ? OrderCycle.find(params[:order_cycle_id]) : nil
|
||||
|
||||
if populate_valid? @distributor
|
||||
order = current_order(true)
|
||||
order.set_distributor! @distributor
|
||||
|
||||
else
|
||||
flash[:error] = "Please choose a distributor for this order." if @distributor.nil?
|
||||
if populate_order_cycle_required
|
||||
flash[:error] = "Please choose a distributor and order cycle for this order." if @distributor.nil? || @order_cycle.nil?
|
||||
else
|
||||
flash[:error] = "Please choose a distributor for this order." if @distributor.nil?
|
||||
end
|
||||
|
||||
redirect_populate_to_first_product
|
||||
end
|
||||
end
|
||||
@@ -108,7 +114,7 @@ Spree::OrdersController.class_eval do
|
||||
return false unless Enterprise.distributing_product(variant.product).include? distributor
|
||||
end if params[:variants]
|
||||
|
||||
# -- If products in cart, distributor can't be changed
|
||||
# -- Distributor can't be changed unless new distributor can service cart
|
||||
order = current_order(false)
|
||||
if !order.nil? && !DistributorChangeValidator.new(order).can_change_to_distributor?(distributor)
|
||||
return false
|
||||
@@ -117,6 +123,18 @@ Spree::OrdersController.class_eval do
|
||||
true
|
||||
end
|
||||
|
||||
# Adding product to cart requires an order cycle if product has no product distributions
|
||||
def populate_order_cycle_required
|
||||
populate_products.any? { |p| p.product_distributions.empty? }
|
||||
end
|
||||
|
||||
def populate_products
|
||||
# TODO: This is quite inefficient. Push to SQLland?
|
||||
(params[:products] || []).map { |product_id, variant_id| Spree::Product.find product_id } +
|
||||
(params[:variants] || []).map { |variant_id, quantity| Spree::Variant.find(variant_id).product }
|
||||
end
|
||||
|
||||
|
||||
def redirect_populate_to_first_product
|
||||
product = if params[:products].present?
|
||||
Spree::Product.find(params[:products].keys.first)
|
||||
|
||||
@@ -162,6 +162,27 @@ feature %q{
|
||||
end
|
||||
|
||||
context "with order cycle distribution" do
|
||||
scenario "adding a product to the cart with no distribution chosen" do
|
||||
# Given a product and some distributors
|
||||
d1 = create(:distributor_enterprise)
|
||||
d2 = create(:distributor_enterprise)
|
||||
p1 = create(:product)
|
||||
p2 = create(:product)
|
||||
create(:simple_order_cycle, :distributors => [d1], :variants => [p1.master])
|
||||
create(:simple_order_cycle, :distributors => [d2], :variants => [p2.master])
|
||||
|
||||
# When I add an item to my cart without choosing a distributor or order cycle
|
||||
visit spree.product_path p1
|
||||
click_button 'Add To Cart'
|
||||
|
||||
# Then I should see an error message
|
||||
page.should have_content "Please choose a distributor and order cycle for this order."
|
||||
|
||||
# And the product should not have been added to my cart
|
||||
Spree::Order.last.should be_nil
|
||||
end
|
||||
|
||||
|
||||
it "allows us to add two products from the same distributor" do
|
||||
# Given two products, each at the same distributor
|
||||
d = create(:distributor_enterprise)
|
||||
|
||||
Reference in New Issue
Block a user