Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Show more specific error when adding item to cart fails (#11650)
Aleksandar Petrushev947117054167
diff --git a/core/app/models/spree/stock/availability_validator.rb b/core/app/models/spree/stock/availability_validator.rbindex 8bc9cf151b..2b70d2b026 100644--- a/core/app/models/spree/stock/availability_validator.rb+++ b/core/app/models/spree/stock/availability_validator.rb@@ -14,9 +14,15 @@ module Spreedisplay_name = variant.name.to_sdisplay_name += " (#{variant.options_text})" unless variant.options_text.blank?- line_item.errors.add(:quantity,- :selected_quantity_not_available,- message: Spree.t(:selected_quantity_not_available, item: display_name.inspect))+ if variant.available?+ line_item.errors.add(:quantity,+ :selected_quantity_not_available,+ message: Spree.t(:selected_quantity_not_available, item: display_name.inspect))+ else+ line_item.errors.add(:base,+ :only_active_products_can_be_added_to_cart,+ message: Spree.t(:only_active_products_can_be_added_to_cart))+ endendprivatediff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 77ed520de6..db521ee52d 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -1122,6 +1122,7 @@ en:number: Numberok: OKon_hand: On Hand+ only_active_products_can_be_added_to_cart: Draft and archived products cannot be added to cart, please mark the product as active before.open: Openopen_all_adjustments: Open All Adjustmentsoption_type: Option Typediff --git a/core/spec/models/spree/stock/availability_validator_spec.rb b/core/spec/models/spree/stock/availability_validator_spec.rbindex 9970d30e75..d2d95a9882 100644--- a/core/spec/models/spree/stock/availability_validator_spec.rb+++ b/core/spec/models/spree/stock/availability_validator_spec.rb@@ -15,7 +15,7 @@ module Spreeallow(inventory_unit).to receive_messages(quantity: 5)end- it 'is valid when supply is sufficient' do+ it 'is valid when supply is sufficient and product is active' doallow_any_instance_of(Stock::Quantifier).to receive_messages(can_supply?: true)expect(line_item).not_to receive(:errors)subject.validate(line_item)@@ -40,6 +40,20 @@ module Spreeexpect(line_item.errors).not_to receive(:[]).with(:quantity)subject.validate(line_item)end++ context 'when supply is sufficient but product is not active' do+ before do+ line_item.product.draft!+ end++ it 'shows a message about product status and not quantity' do+ allow_any_instance_of(Stock::Quantifier).to receive_messages(can_supply?: false)+ expect(line_item.errors[:base]).to eq []+ subject.validate(line_item)+ expect(line_item.errors[:base].to_s).to match(/mark the product as active/)+ expect(line_item.errors[:quantity]).to eq []+ end+ endendendend