show specific error message when add-to-cart fails

This commit is contained in:
Andy Brett
2020-11-28 10:01:17 -08:00
parent 0824f12472
commit c70ea44091
4 changed files with 7 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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