From 7ecdc7e0bb3fabbdc6e711a824b768a7d161e27d Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 23 Nov 2022 15:42:05 +0100 Subject: [PATCH] Make split_promotions_with_any_match_policy task faster Do not re-apply promotions to incomplete orders if there are no promotions effected. This is not necessary and speeds up the task for shop with lots of incomplete orders (this could easily be in the thousands). Also make the effected promotions update faster by using find_each and include actions as well. --- .../solidus/split_promotions_with_any_match_policy.rake | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/lib/tasks/solidus/split_promotions_with_any_match_policy.rake b/core/lib/tasks/solidus/split_promotions_with_any_match_policy.rake index ece805597d5..7018a8e5fda 100644 --- a/core/lib/tasks/solidus/split_promotions_with_any_match_policy.rake +++ b/core/lib/tasks/solidus/split_promotions_with_any_match_policy.rake @@ -3,7 +3,8 @@ namespace :solidus do desc "Split Promotions with 'any' match policy" task split_promotions_with_any_match_policy: :environment do - Spree::Promotion.where(match_policy: :any).includes(:promotion_rules).all.each do |promotion| + promotions = Spree::Promotion.where(match_policy: :any) + promotions.includes(:promotion_rules, :promotion_actions).find_each do |promotion| if promotion.promotion_rules.length <= 1 promotion.update!(match_policy: :all) elsif promotion.active? @@ -28,6 +29,8 @@ namespace :solidus do end end - Spree::Order.where(completed_at: nil).each { |order| Spree::PromotionHandler::Cart.new(order).activate } + if promotions.any? + Spree::Order.where(completed_at: nil).each { |order| Spree::PromotionHandler::Cart.new(order).activate } + end end end