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
Add warning on shipping rates deletion (#14219)
Kacper Mękarskic254eed332d8
diff --git a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rbindex 323f317f76..e4f318541e 100644--- a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb@@ -486,6 +486,27 @@ RSpec.describe Spree::Api::V3::Store::CartsController, type: :controller doexpect(json_response['fulfillments']).to be_presentend+ it 'surfaces a delivery_unavailable warning when the cart cannot be delivered' do+ address = create(:address, user: user, country: country, state: us_state)+ cart.update!(email: '[email protected]', ship_address: address)+ cart.shipments.delete_all+ cart.update_column(:state, 'address')++ # Move the products into a shipping category no shipping method serves.+ unserved_category = create(:shipping_category, name: 'Unserved')+ cart.line_items.each { |line_item| line_item.variant.product.update!(shipping_category: unserved_category) }+ cart.reload++ get :show, params: { id: cart.prefixed_id }++ expect(response).to have_http_status(:ok)+ expect(json_response['fulfillments']).to be_empty+ expect(json_response['warnings'].map { |warning| warning['code'] }).to include('delivery_unavailable')+ expect(json_response['requirements']).to include(+ a_hash_including('step' => 'delivery', 'field' => 'shipping_method')+ )+ end+it 'does not advance when no address is set' docart.update!(ship_address: nil)cart.shipments.delete_all@@ -521,6 +542,27 @@ RSpec.describe Spree::Api::V3::Store::CartsController, type: :controller doexpect(order.reload.ship_address_id).to eq(existing_address.id)end+ context 'when a line item cannot be delivered to the address' do+ let(:address) { user.addresses.first || create(:address, user: user, country: country, state: us_state) }+ let(:unserved_category) { create(:shipping_category, name: 'Unserved') }++ before do+ order.update!(email: '[email protected]')+ order.line_items.each { |line_item| line_item.variant.product.update!(shipping_category: unserved_category) }+ end++ it 'returns ok with a delivery_unavailable warning, an unmet delivery requirement, and no fulfillments' do+ patch :update, params: { id: order.prefixed_id, shipping_address_id: address.prefixed_id }++ expect(response).to have_http_status(:ok)+ expect(json_response['fulfillments']).to be_empty+ expect(json_response['warnings'].map { |warning| warning['code'] }).to include('delivery_unavailable')+ expect(json_response['requirements']).to include(+ a_hash_including('step' => 'delivery', 'field' => 'shipping_method')+ )+ end+ end+context 'updating market' dolet!(:market) { create(:market, store: store, countries: [country]) }diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rbindex a7b682c108..5bfefee289 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -62,17 +62,20 @@ module Spree[outstanding_balance - total_applied_store_credit, 0].maxend- # Transient warnings populated by remove_out_of_stock_items!+ # Transient warnings populated by remove_out_of_stock_items! and ensure_available_shipping_ratesattribute :warnings, default: -> { [] }# Removes out-of-stock/discontinued items and populates warnings.# Returns self (reloaded if items were removed) with warnings set.+ # Captured before the call because removing items reloads the order, which+ # would drop warnings already recorded upstream.def remove_out_of_stock_items!+ existing_warnings = warningsresult = Spree::Cart::RemoveOutOfStockItems.call(order: self)return self unless result.success?- order, _messages, warnings = result.value- order.warnings = warnings || []+ order, _messages, new_warnings = result.value+ order.warnings = existing_warnings | (new_warnings || [])orderend@@ -1081,8 +1084,17 @@ module Spreeif line_items_without_shipping_rates.present?errors.add(:base, Spree.t(:products_cannot_be_shipped, product_names: line_items_without_shipping_rates.map(&:name).to_sentence))+ self.warnings |= line_items_without_shipping_rates.map do |line_item|+ {+ code: 'delivery_unavailable',+ message: Spree.t('cart_line_item.delivery_unavailable', li_name: line_item.name),+ line_item_id: line_item.prefixed_id,+ variant_id: line_item.variant&.prefixed_id+ }+ endelseerrors.add(:base, Spree.t(:items_cannot_be_shipped))+ self.warnings |= [{ code: 'delivery_unavailable', message: Spree.t(:items_cannot_be_shipped) }]endfalsediff --git a/spree/core/app/services/spree/carts/update.rb b/spree/core/app/services/spree/carts/update.rbindex 00b378ce8d..6e17c882eb 100644--- a/spree/core/app/services/spree/carts/update.rb+++ b/spree/core/app/services/spree/carts/update.rb@@ -144,11 +144,14 @@ module Spreerescue StandardError => eRails.error.report(e, context: { order_id: cart.id, state: cart.state }, source: 'spree.checkout')ensure+ # A halted transition records warnings on the cart, which reload would drop, so carry them across the reload.+ warnings = cart.warningsbegincart.reloadrescue StandardError # rubocop:disable Lint/SuppressedException# reload failure must not mask the original resultend+ cart.warnings |= warnings if warnings.present?endendenddiff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex 429790b2fa..09694c4547 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -728,6 +728,7 @@ en:cart: Cartcart_already_updated: The cart has already been updated.cart_line_item:+ delivery_unavailable: "%{li_name} cannot be delivered to the selected address"discontinued: "%{li_name} was removed because it was discontinued"out_of_stock: "%{li_name} was removed because it was sold out"categories: Categoriesdiff --git a/spree/core/spec/services/spree/carts/update_spec.rb b/spree/core/spec/services/spree/carts/update_spec.rbindex 47ebe8804c..cca4da892e 100644--- a/spree/core/spec/services/spree/carts/update_spec.rb+++ b/spree/core/spec/services/spree/carts/update_spec.rb@@ -627,6 +627,32 @@ module Spreeendend+ describe 'when the cart cannot be delivered' do+ let!(:order) { create(:order_with_line_items, user: user, store: store, state: 'address') }+ let(:params) { { email: '[email protected]' } }+ let(:unserved_category) { create(:shipping_category, name: 'Unserved') }++ before do+ order.line_items.each { |line_item| line_item.variant.product.update!(shipping_category: unserved_category) }+ order.reload+ end++ it 'keeps the cart on the address step with no fulfillments' do+ expect(subject).to be_success+ expect(order.reload.state).to eq('address')+ expect(order.shipments).to be_empty+ end++ it 'surfaces a delivery_unavailable warning for each undeliverable line item' do+ warnings = subject.value.warnings++ expect(warnings).to be_present+ expect(warnings).to all(include(code: 'delivery_unavailable'))+ expect(warnings.map { |warning| warning[:line_item_id] })+ .to match_array(order.line_items.map(&:prefixed_id))+ end+ end+endendend