Skip to content

Commit

Permalink
Make split_promotions_with_any_match_policy task faster
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tvdeyen committed Nov 23, 2022
1 parent d582f54 commit 7ecdc7e
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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

0 comments on commit 7ecdc7e

Please sign in to comment.