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
Improve `Order#ensure_available_shipping_rates` to also record which items cannot be shipped
Damian Legawiec8d39238aaff7
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex e60fb2f148..b5c04b6ab9 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -137,8 +137,6 @@ module Spreeendhas_many :shipment_adjustments, through: :shipments, source: :adjustments- attribute :invalid_line_items, array: true, default: []-accepts_nested_attributes_for :line_itemsaccepts_nested_attributes_for :bill_addressaccepts_nested_attributes_for :ship_address@@ -593,6 +591,22 @@ module Spree@total_weight ||= line_items.joins(:variant).includes(:variant).map { |li| li.variant.weight * li.quantity }.sumend+ # Returns line items that have no shipping rates+ #+ # @return [Array<Spree::LineItem>]+ def line_items_without_shipping_rates+ @line_items_without_shipping_rates ||= shipments.map do |shipment|+ shipment.manifest.map(&:line_item) if shipment.shipping_rates.blank?+ end.flatten.compact+ end++ # Checks if all line items cannot be shipped+ #+ # @returns Boolean+ def all_line_items_invalid?+ line_items_without_shipping_rates.size == line_items.count+ end+def apply_free_shipping_promotionsSpree::PromotionHandler::FreeShipping.new(self).activateshipments.each { |shipment| Spree::Adjustable::AdjustmentsUpdater.update(shipment) }@@ -801,11 +815,18 @@ module Spreeenddef ensure_available_shipping_rates- if shipments.empty? || shipments.any? { |shipment| shipment.shipping_rates.blank? }+ if shipments.empty? || line_items_without_shipping_rates.present?# After this point, order redirects back to 'address' state and asks user to pick a proper address# Therefore, shipments are not necessary at this point.shipments.destroy_all- errors.add(:base, Spree.t(:items_cannot_be_shipped)) && (return false)++ if 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))+ else+ errors.add(:base, Spree.t(:items_cannot_be_shipped))+ end++ return falseendenddiff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 181814fc05..fdb5ddc89c 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -1006,6 +1006,7 @@ en:lt: less thanlte: less than or equal toitems_cannot_be_shipped: We are unable to calculate shipping rates for the selected items.+ products_cannot_be_shipped: We cannot ship %{product_names} to your current location at the moment. Please change your shipping address or remove %{product_names} from your cart.items_in_rmas: Items in Return Authorizationsitems_to_be_reimbursed: Items to be reimburseditems_reimbursed: Items reimburseddiff --git a/core/spec/models/spree/order/checkout_spec.rb b/core/spec/models/spree/order/checkout_spec.rbindex 8a43043b93..7b18a7128e 100644--- a/core/spec/models/spree/order/checkout_spec.rb+++ b/core/spec/models/spree/order/checkout_spec.rb@@ -235,14 +235,11 @@ describe Spree::Order, type: :model docontext 'cannot transition to delivery' docontext 'with an existing shipment' do- before do- line_item = FactoryBot.create(:line_item, price: 10)- order.line_items << line_item- end+ let!(:line_item) { create(:line_item, price: 10, order: order) }context 'if there are no shipping rates for any shipment' doit 'raises an InvalidTransitionError' do- expect { order.next! }.to raise_error(StateMachines::InvalidTransition, /#{Spree.t(:items_cannot_be_shipped)}/)+ expect { order.next! }.to raise_error(StateMachines::InvalidTransition, /#{Spree.t(:products_cannot_be_shipped, product_names: line_item.name)}/)endit 'deletes all the shipments' dodiff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rbindex 8e26f70172..8c71bff104 100644--- a/core/spec/models/spree/order_spec.rb+++ b/core/spec/models/spree/order_spec.rb@@ -1979,11 +1979,78 @@ describe Spree::Order, type: :model doendend- describe '#remove_invalid_line_items' do- let(:order) { create(:order_with_line_items, store: store) }+ describe '#line_items_without_shipping_rates' do+ subject { order.line_items_without_shipping_rates }- it 'removes invalid line items' do- expect { order.remove_invalid_line_items(order.line_items.pluck(:id)) }.to change { order.line_items.count }.from(1).to(0)+ let(:order) { create(:order_with_line_items) }+ let(:shipment) { order.shipments.first }+ let(:line_item) { order.line_items.first }++ context 'when order has no shipments' do+ it 'returns an empty array' do+ expect(subject).to eq([])+ end+ end++ context 'when order has shipments with no shipping rates' do+ before do+ shipment.shipping_rates.destroy_all+ end++ it 'returns the line items without shipping rates' do+ expect(subject).to eq([line_item])+ end+ end++ context 'when order has shipments with shipping rates' do+ let!(:shipping_rate) { create(:shipping_rate, shipment: shipment) }++ it 'returns an empty array' do+ expect(subject).to eq([])+ end+ end+ end++ describe '#ensure_available_shipping_rates' do+ subject { order.send(:ensure_available_shipping_rates) }++ let(:order) { create(:order_with_line_items) }+ let(:line_item) { order.line_items.first }+ let(:shipment) { order.shipments.first }++ context 'when order has no shipments' do+ before do+ order.shipments.destroy_all+ end++ it 'returns false and adds an error to the order' do+ expect(subject).to be false+ expect(order.errors.full_messages).to include(Spree.t(:items_cannot_be_shipped))+ end+ end++ context 'when order has shipments with no shipping rates' do+ before do+ shipment.shipping_rates.destroy_all+ end++ it 'returns false and adds an error to the order' do+ expect(subject).to be false+ expect(order.errors.full_messages).to include(Spree.t(:products_cannot_be_shipped, product_names: line_item.name))+ end++ it 'deletes all the shipments' do+ expect { subject }.to change(order.shipments, :count).to(0)+ end+ end++ context 'when order has shipments with shipping rates' do+ let!(:shipping_rate) { create(:shipping_rate, shipment: shipment) }++ it 'returns nil and does not add an error to the order' do+ expect(subject).to be_nil+ expect(order.errors.full_messages).to be_empty+ endendendenddiff --git a/storefront/app/controllers/spree/checkout_controller.rb b/storefront/app/controllers/spree/checkout_controller.rbindex 046d23ea60..682ca94588 100644--- a/storefront/app/controllers/spree/checkout_controller.rb+++ b/storefront/app/controllers/spree/checkout_controller.rb@@ -57,7 +57,7 @@ module Spreeunless params[:do_not_advance]@order.temporary_address = !params[:save_user_address]unless @order.next- return if @order.address? && @order.invalid_line_items.any? && turbo_stream_request? # render update trubo_stream+ return if @order.address? && @order.line_items_without_shipping_rates.any? && turbo_stream_request? # render update trubo_streamflash[:error] = @order.errors.messages.values.flatten.join("\n")redirect_to(spree.checkout_state_path(@order.token, @order.state)) && returndiff --git a/storefront/app/views/spree/checkout/_missing_line_items.html.erb b/storefront/app/views/spree/checkout/_missing_line_items.html.erbindex b4c38180ec..891fd5b04b 100644--- a/storefront/app/views/spree/checkout/_missing_line_items.html.erb+++ b/storefront/app/views/spree/checkout/_missing_line_items.html.erb@@ -5,25 +5,17 @@<div class="p-8"><h2 class="text-xl mb-4"><%= Spree.t('storefront.checkout.shipping_not_available') %></h2><p class="mb-4">- <%= Spree.t('checkout.errors.invalid_line_items', count: @order.invalid_line_items.size) %>+ <%= Spree.t('checkout.errors.invalid_line_items', count: @order.line_items_without_shipping_rates.size) %></p><div class="mb-5 overflow-y-auto pt-3 mx-3 lg:mx-8 lg:mt-4 lg:pt-6 line-items" data-checkout-summary-target="line_items">- <% if @order.vendors.any? || @order.splitted? %>- <% @order.invalid_line_items.each do |line_item| %>- <div class="mb-5">- <%= render collection: line_items, partial: 'spree/checkout/line_item', cached: spree_base_cache_scope %>- </div>- <% end %>- <% else %>- <%= render collection: @order.invalid_line_items, partial: 'spree/checkout/line_item', cached: spree_base_cache_scope %>- <% end %>+ <%= render collection: @order.line_items_without_shipping_rates, partial: 'spree/checkout/line_item', cached: spree_base_cache_scope %></div><div class="flex justify-end items-center flex-wrap mt-6"><%= link_to Spree.t('storefront.checkout.continue_to_cart'), spree.cart_path, class: 'mx-4 py-4', target: :_top %><%= button_to Spree.t('storefront.checkout.continue_to_checkout'),- spree.checkout_remove_missing_items_path(@order.token, line_item_ids: @order.invalid_line_items.map(&:id)),+ spree.checkout_remove_missing_items_path(@order.token, line_item_ids: @order.line_items_without_shipping_rates.map(&:id)),class: 'btn-primary font-semibold checkout-content-save-continue-button my-5 ml-4 !py-4',method: :delete,data: { turbo: false, 'turbo-submits-with': (render 'spree/checkout/button_processing', is_hidden: false) }diff --git a/storefront/app/views/spree/checkout/update.turbo_stream.erb b/storefront/app/views/spree/checkout/update.turbo_stream.erbindex b2851bca90..6f7296fb9a 100644--- a/storefront/app/views/spree/checkout/update.turbo_stream.erb+++ b/storefront/app/views/spree/checkout/update.turbo_stream.erb@@ -9,14 +9,14 @@<%= turbo_stream.update 'summary-order-total' do %><%= @order.display_total.to_html %><% end %>-<% if @order.invalid_line_items.any? && [email protected]_line_items_invalid? %>+<% if @order.line_items_without_shipping_rates.any? && [email protected]_line_items_invalid? %><%= turbo_stream.append :modal do %><%= turbo_frame_tag :modal do %><%= render partial: 'spree/checkout/missing_line_items' %><% end %><% end %><% end %>-<% if @order.invalid_line_items.any? && @order.all_line_items_invalid? %>+<% if @order.line_items_without_shipping_rates.any? && @order.all_line_items_invalid? %><%= turbo_stream.append :modal do %><%= turbo_frame_tag :modal do %><%= render partial: 'spree/checkout/missing_all_line_items' %>diff --git a/storefront/spec/controllers/spree/checkout_controller_spec.rb b/storefront/spec/controllers/spree/checkout_controller_spec.rbindex 3c3af42063..fa5a9e36c8 100644--- a/storefront/spec/controllers/spree/checkout_controller_spec.rb+++ b/storefront/spec/controllers/spree/checkout_controller_spec.rb@@ -728,7 +728,7 @@ describe Spree::CheckoutController, type: :controller doorder.shipments.first.shipping_rates.delete_allput :update, params: { state: order.state, order: {}, token: order.token }- expect(flash[:error]).to eq(Spree.t(:items_cannot_be_shipped, product_names: order.products.pluck(:name).to_sentence))+ expect(flash[:error]).to eq(Spree.t(:products_cannot_be_shipped, product_names: order.products.pluck(:name).to_sentence))expect(response).to redirect_to(spree.checkout_state_path(order.token, 'address'))endend