From c70ea44091c8558c685e9e4c9437d89078d08d52 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Sat, 28 Nov 2020 10:01:17 -0800 Subject: [PATCH] show specific error message when add-to-cart fails --- app/assets/javascripts/darkswarm/services/cart.js.coffee | 2 +- app/controllers/cart_controller.rb | 5 +++-- app/services/cart_service.rb | 8 ++------ config/locales/en.yml | 1 + 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index b8aa5c8727..9fbb6ca415 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -50,7 +50,7 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, $modal, $roo @popQueue() if @update_enqueued .error (response, status)=> - Messages.flash({error: t('js.cart.add_to_cart_failed')}) + Messages.flash({error: response.error}) @update_running = false compareAndNotifyStockLevels: (stockLevels) => diff --git a/app/controllers/cart_controller.rb b/app/controllers/cart_controller.rb index b65fb09cde..c7c1a32905 100644 --- a/app/controllers/cart_controller.rb +++ b/app/controllers/cart_controller.rb @@ -11,7 +11,8 @@ class CartController < BaseController Spree::Adjustment.without_callbacks do cart_service = CartService.new(order) - if cart_service.populate(params.slice(:products, :variants, :quantity), true) + errors = cart_service.populate(params.slice(:products, :variants, :quantity), true)[:errors] + if errors.blank? order.update_distribution_charge! order.cap_quantity_at_stock! order.update! @@ -22,7 +23,7 @@ class CartController < BaseController stock_levels: VariantsStockLevels.new.call(order, variant_ids) }, status: :ok else - render json: { error: true }, status: :precondition_failed + render json: { error: errors.full_messages.join(",") }, status: :precondition_failed end end populate_variant_attributes diff --git a/app/services/cart_service.rb b/app/services/cart_service.rb index d46e2478ca..f15396843e 100644 --- a/app/services/cart_service.rb +++ b/app/services/cart_service.rb @@ -19,7 +19,7 @@ class CartService attempt_cart_add_variants variants_data overwrite_variants variants_data if overwrite end - valid? + { errors: errors } end private @@ -109,10 +109,6 @@ class CartService end end - def valid? - errors.empty? - end - def cart_remove(variant_id) variant = Spree::Variant.find(variant_id) @order.remove_variant(variant) @@ -145,7 +141,7 @@ class CartService def check_order_cycle_provided order_cycle_provided = @order_cycle.present? - errors.add(:base, "Please choose an order cycle for this order.") unless order_cycle_provided + errors.add(:base, I18n.t(:spree_order_cycle_error)) unless order_cycle_provided order_cycle_provided end diff --git a/config/locales/en.yml b/config/locales/en.yml index 8b5a184821..6ce184d77f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2214,6 +2214,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using spree_classification_primary_taxon_error: "Taxon %{taxon} is the primary taxon of %{product} and cannot be deleted" spree_order_availability_error: "Distributor or order cycle cannot supply the products in your cart" spree_order_populator_error: "That distributor or order cycle can't supply all the products in your cart. Please choose another." + spree_order_cycle_error: "Please choose an order cycle for this order." spree_order_populator_availability_error: "That product is not available from the chosen distributor or order cycle." spree_distributors_error: "At least one hub must be selected" spree_user_enterprise_limit_error: "^%{email} is not permitted to own any more enterprises (limit is %{enterprise_limit})."