diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 90b851fbaf..5d6fb8fb18 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -1,5 +1,6 @@ Spree::OrdersController.class_eval do before_filter :populate_order_distributor, :only => :populate + before_filter :populate_order_count_on_hand, :only => :populate after_filter :populate_variant_attributes, :only => :populate def populate_order_distributor @@ -15,6 +16,24 @@ Spree::OrdersController.class_eval do end end + def populate_order_count_on_hand + params[:products].each do |product_id, variant_id| + product = Spree::Product.find product_id + if product.count_on_hand < params[:quantity].to_i + flash[:error] = "Unfortunately " + (product.count_on_hand == 0 ? "no" : "only" + product.count_on_hand.to_s ) + " units of the selected item remain." + redirect_populate_to_first_product + end + end if params[:products] + + params[:variants].each do |variant_id, quantity| + variant = Spree::Variant.find variant_id + if variant.count_on_hand < params[:quantity].to_i + flash[:error] = "Unfortunately " + (variant.count_on_hand == 0 ? "no" : "only" + variant.count_on_hand.to_s ) + " units of the selected item remain." + redirect_populate_to_first_product + end + end if params[:variants] + end + def populate_variant_attributes if params.key? :variant_attributes params[:variant_attributes].each do |variant_id, attributes|