From 5b9db50250e6b254ad65e15a71495f61be50154a Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 8 Dec 2020 11:21:05 +0000 Subject: [PATCH] Optimise Shipment#to_package This is done in a later Spree commit to reduce the amount of processing done in larger orders. See: https://github.com/spree/spree/commit/ab01b1ec1e2b08068b3bfcca99c7886c0747514f --- app/models/spree/shipment.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index 6763de4712b..84c717a8190 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -249,8 +249,11 @@ def inventory_units_for(variant) def to_package package = OrderManagement::Stock::Package.new(stock_location, order) - inventory_units.includes(:variant).each do |inventory_unit| - package.add inventory_unit.variant, 1, inventory_unit.state_name + grouped_inventory_units = inventory_units.includes(:variant).group_by do |iu| + [iu.variant, iu.state_name] + end + grouped_inventory_units.each do |(variant, state_name), inventory_units| + package.add variant, inventory_units.count, state_name end package end