diff --git a/core/app/models/spree/stock/availability.rb b/core/app/models/spree/stock/availability.rb index cd0cfdf0586..dd3c2322965 100644 --- a/core/app/models/spree/stock/availability.rb +++ b/core/app/models/spree/stock/availability.rb @@ -19,7 +19,7 @@ def initialize(variants:, stock_locations: Spree::StockLocation.active) # Get the on_hand stock quantities # @return [HashSpree::StockQuantities>] A map of stock_location_ids to the stock quantities available in that location def on_hand_by_stock_location_id - counts_on_hand.to_a.group_by do |(_, stock_location_id), _| + on_hand_quantities = counts_on_hand.to_a.group_by do |(_, stock_location_id), _| stock_location_id end.transform_values do |values| Spree::StockQuantities.new( @@ -31,12 +31,13 @@ def on_hand_by_stock_location_id end.to_h ) end + stock_location_sorter(on_hand_quantities) end # Get the on_hand stock quantities # @return [HashSpree::StockQuantities>] A map of stock_location_ids to the stock quantities available in that location def backorderable_by_stock_location_id - backorderables.group_by(&:second).transform_values do |variant_ids| + backorderable_quantities = backorderables.group_by(&:second).transform_values do |variant_ids| Spree::StockQuantities.new( variant_ids.map do |variant_id, _| variant = @variant_map[variant_id] @@ -44,6 +45,7 @@ def backorderable_by_stock_location_id end.to_h ) end + stock_location_sorter(backorderable_quantities) end private @@ -67,6 +69,12 @@ def stock_item_scope where(variant_id: @variants). where(stock_location_id: @stock_locations) end + + def stock_location_sorter(stock_quantities) + location_ids = @stock_locations.map(&:id) + + stock_quantities.sort_by { |stock_quantity| location_ids.index(stock_quantity[0]) }.to_h + end end end end diff --git a/core/app/models/spree/stock/simple_coordinator.rb b/core/app/models/spree/stock/simple_coordinator.rb index 6b0fe0d5786..6e334982ab2 100644 --- a/core/app/models/spree/stock/simple_coordinator.rb +++ b/core/app/models/spree/stock/simple_coordinator.rb @@ -87,16 +87,6 @@ def split_packages(initial_packages) end end - def sort_availability(availability) - sorted_availability = availability.sort_by do |stock_location_id, _| - @stock_locations.find_index do |stock_location| - stock_location.id == stock_location_id - end - end - - Hash[sorted_availability] - end - def get_units(quantities) # Change our raw quantities back into inventory units quantities.flat_map do |variant, quantity|