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.

Reduce the number of SQL queries for `OrderUpdater#update_adjustment_… (#13399)

Damian Legawieccd1010b8cf5e
diff --git a/core/app/models/spree/order_updater.rb b/core/app/models/spree/order_updater.rb
index aaebdd9692..d66b4958c2 100644
--- a/core/app/models/spree/order_updater.rb
+++ b/core/app/models/spree/order_updater.rb
@@ -79,15 +79,34 @@ module Spree
def update_adjustment_total
recalculate_adjustments
- order.adjustment_total = line_items.sum(:adjustment_total) +
- shipments.sum(:adjustment_total) +
- adjustments.eligible.sum(:amount)
- order.included_tax_total = line_items.sum(:included_tax_total) + shipments.sum(:included_tax_total)
- order.additional_tax_total = line_items.sum(:additional_tax_total) + shipments.sum(:additional_tax_total)
-
- order.promo_total = line_items.sum(:promo_total) +
- shipments.sum(:promo_total) +
- adjustments.promotion.eligible.sum(:amount)
+
+ # Fetch all line item totals in a single query
+ # Use reorder(nil) to remove default ordering which conflicts with aggregates in PostgreSQL
+ line_item_totals = line_items.reorder(nil).pick(
+ Arel.sql('COALESCE(SUM(adjustment_total), 0)'),
+ Arel.sql('COALESCE(SUM(included_tax_total), 0)'),
+ Arel.sql('COALESCE(SUM(additional_tax_total), 0)'),
+ Arel.sql('COALESCE(SUM(promo_total), 0)')
+ ) || [0, 0, 0, 0]
+
+ # Fetch all shipment totals in a single query
+ shipment_totals = shipments.reorder(nil).pick(
+ Arel.sql('COALESCE(SUM(adjustment_total), 0)'),
+ Arel.sql('COALESCE(SUM(included_tax_total), 0)'),
+ Arel.sql('COALESCE(SUM(additional_tax_total), 0)'),
+ Arel.sql('COALESCE(SUM(promo_total), 0)')
+ ) || [0, 0, 0, 0]
+
+ # Fetch order-level adjustment totals in a single query
+ order_adjustment_totals = adjustments.eligible.reorder(nil).pick(
+ Arel.sql('COALESCE(SUM(amount), 0)'),
+ Arel.sql("COALESCE(SUM(CASE WHEN source_type = 'Spree::PromotionAction' THEN amount ELSE 0 END), 0)")
+ ) || [0, 0]
+
+ order.adjustment_total = line_item_totals[0] + shipment_totals[0] + order_adjustment_totals[0]
+ order.included_tax_total = line_item_totals[1] + shipment_totals[1]
+ order.additional_tax_total = line_item_totals[2] + shipment_totals[2]
+ order.promo_total = line_item_totals[3] + shipment_totals[3] + order_adjustment_totals[1]
update_order_total
end