diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 4aea409444..0a7d146748 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -21,14 +21,15 @@ Spree::OrdersController.class_eval do def edit @order = current_order(true) @insufficient_stock_lines = @order.insufficient_stock_lines + @unavailable_order_variants = OrderCycleDistributedVariants.new(current_order_cycle, current_distributor).unavailable_order_variants(@order) if @order.line_items.empty? redirect_to main_app.shop_path else associate_user - if @order.insufficient_stock_lines.present? - flash[:error] = t("spree.inventory_error_flash_for_insufficient_quantity") + if @order.insufficient_stock_lines.present? || @unavailable_order_variants.present? + flash[:error] = t("spree.orders.error_flash_for_unavailable_items") end end end diff --git a/app/services/order_cycle_distributed_variants.rb b/app/services/order_cycle_distributed_variants.rb index 0718b9b393..abaa3303e4 100644 --- a/app/services/order_cycle_distributed_variants.rb +++ b/app/services/order_cycle_distributed_variants.rb @@ -5,7 +5,11 @@ class OrderCycleDistributedVariants end def distributes_order_variants?(order) - (order.line_item_variants - available_variants).empty? + unavailable_order_variants(order).empty? + end + + def unavailable_order_variants(order) + order.line_item_variants - available_variants end def available_variants diff --git a/app/views/spree/orders/_line_item.html.haml b/app/views/spree/orders/_line_item.html.haml index 1194bed136..ac1396fd14 100644 --- a/app/views/spree/orders/_line_item.html.haml +++ b/app/views/spree/orders/_line_item.html.haml @@ -14,6 +14,11 @@ = variant.in_stock? ? t(".insufficient_stock", :on_hand => variant.on_hand) : t(".out_of_stock") %br/ + - if @unavailable_order_variants.andand.include? line_item.variant + %span.out-of-stock + = t(".unavailable_item") + %br/ + %td.text-right.cart-item-price{"data-hook" => "cart_item_price"} = line_item.single_display_amount_with_adjustments.to_html %td.text-center.cart-item-quantity{"data-hook" => "cart_item_quantity"} diff --git a/config/locales/en.yml b/config/locales/en.yml index eb2885c416..c26e0da260 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3073,6 +3073,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using format: ! '%Y-%m-%d' js_format: 'yy-mm-dd' orders: + error_flash_for_unavailable_items: "An item in your cart has become unavailable." edit: login_to_view_order: "Please log in to view your order." bought: @@ -3080,6 +3081,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using line_item: insufficient_stock: "Insufficient stock available, only %{on_hand} remaining" out_of_stock: "Out of Stock" + unavailable_item: "Currently unavailable" shipment_states: backorder: backorder partial: partial diff --git a/spec/controllers/spree/orders_controller_spec.rb b/spec/controllers/spree/orders_controller_spec.rb index 071e463979..63a4bd5680 100644 --- a/spec/controllers/spree/orders_controller_spec.rb +++ b/spec/controllers/spree/orders_controller_spec.rb @@ -89,6 +89,9 @@ describe Spree::OrdersController, type: :controller do allow(controller).to receive(:current_order).and_return order allow(order).to receive_message_chain(:line_items, :empty?).and_return true allow(order).to receive(:insufficient_stock_lines).and_return [] + allow(order).to receive(:line_item_variants).and_return [] + allow(order_cycle).to receive(:variants_distributed_by).and_return [] + session[:access_token] = order.token spree_get :edit expect(response).to redirect_to shop_path @@ -161,6 +164,18 @@ describe Spree::OrdersController, type: :controller do expect(flash[:error]).to eq("An item in your cart has become unavailable.") end end + + describe "when an item is unavailable" do + before do + order.order_cycle = create(:simple_order_cycle, distributors: [d], variants: []) + end + + it "displays a flash message when we view the cart" do + spree_get :edit + expect(response.status).to eq 200 + expect(flash[:error]).to eq("An item in your cart has become unavailable.") + end + end end end