Add flash and warning to the cart page when item becomes unavailable

This commit is contained in:
luisramos0
2019-04-13 20:44:10 +01:00
parent d602375ac7
commit fce3d69345
5 changed files with 30 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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"}

View File

@@ -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

View File

@@ -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