Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 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 Spree
display_name = variant.name.to_s
display_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))
+ end
end
private
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 77ed520de6..db521ee52d 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1122,6 +1122,7 @@ en:
number: Number
ok: OK
on_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: Open
open_all_adjustments: Open All Adjustments
option_type: Option Type
diff --git a/core/spec/models/spree/stock/availability_validator_spec.rb b/core/spec/models/spree/stock/availability_validator_spec.rb
index 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 Spree
allow(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' do
allow_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 Spree
expect(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
+ end
end
end
end