mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Do not add the product to cart if the user specifies a distributor that the product is not available at
This commit is contained in:
@@ -3,9 +3,26 @@ Spree::OrdersController.class_eval do
|
||||
|
||||
def populate_order_distributor
|
||||
@distributor = Spree::Distributor.find params[:distributor_id]
|
||||
if @distributor.nil?
|
||||
return false
|
||||
end
|
||||
|
||||
redirect_to cart_path unless populate_valid? @distributor
|
||||
end
|
||||
|
||||
private
|
||||
def populate_valid? distributor
|
||||
# -- Distributor must be specified
|
||||
return false if distributor.nil?
|
||||
|
||||
# -- All products must be available under that distributor
|
||||
params[:products].each do |product_id, variant_id|
|
||||
product = Spree::Product.find product_id
|
||||
return false unless product.distributors.include? distributor
|
||||
end if params[:products]
|
||||
|
||||
params[:variants].each do |variant_id, quantity|
|
||||
variant = Spree::Variant.find variant_id
|
||||
return false unless variant.product.distributors.include? distributor
|
||||
end if params[:variants]
|
||||
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@ require 'spec_helper'
|
||||
|
||||
describe Spree::OrdersController do
|
||||
context "adding the first product to the cart" do
|
||||
it "does nothing if the user does not specify a distributor" do
|
||||
it "does not add the product if the user does not specify a distributor" do
|
||||
create(:distributor)
|
||||
p = create(:product)
|
||||
|
||||
@@ -10,5 +10,15 @@ describe Spree::OrdersController do
|
||||
spree_put :populate, :variants => {p.id => 1}
|
||||
end.to change(Spree::LineItem, :count).by(0)
|
||||
end
|
||||
|
||||
it "does not add the product if the user specifies a distributor that the product is not available at" do
|
||||
distributor_product = create(:distributor)
|
||||
distributor_no_product = create(:distributor)
|
||||
p = create(:product, :distributors => [distributor_product])
|
||||
|
||||
expect do
|
||||
spree_put :populate, :variants => {p.id => 1}, :distributor_id => distributor_no_product.id
|
||||
end.to change(Spree::LineItem, :count).by(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user