-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This module handles discounts from the order updater. The system will check all line items and shipments for any applicable discounts and select the best for each line item / shipment in an understandable, performant way.
- Loading branch information
Showing
7 changed files
with
157 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Discounts | ||
class Chooser | ||
def initialize(_discountable) | ||
# This signature is here to provide context in case | ||
# this needs to be customized | ||
end | ||
|
||
def call(discounts) | ||
[discounts.max_by(&:amount)].compact | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Discounts | ||
class LineItemDiscounter | ||
attr_reader :promotions | ||
|
||
def initialize(promotions:) | ||
@promotions = promotions | ||
end | ||
|
||
def call(line_item) | ||
discounts = promotions.select do |promotion| | ||
promotion.line_item_discountable?(line_item) | ||
end.flat_map do |promotion| | ||
promotion.actions.select do |action| | ||
action.discountable_class == Spree::LineItem | ||
end.map do |action| | ||
action.discount(line_item) | ||
end | ||
end | ||
|
||
chosen_discounts = Spree::Config.discount_chooser_class.new(line_item).call(discounts) | ||
line_item.discounts = chosen_discounts | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Discounts | ||
class OrderDiscounter | ||
attr_reader :order | ||
|
||
def initialize(order) | ||
@order = order | ||
end | ||
|
||
def call | ||
discount_line_items | ||
discount_shipments | ||
end | ||
|
||
private | ||
|
||
def discount_line_items | ||
line_item_promotions = promotions.select do |promotion| | ||
promotion.actions.any? { |promotion_action| promotion_action.discountable_class == Spree::LineItem } | ||
end | ||
line_item_discounter = LineItemDiscounter.new(promotions: line_item_promotions) | ||
order.line_items.each { |line_item| line_item_discounter.call(line_item) } | ||
end | ||
|
||
def discount_shipments | ||
shipment_promotions = promotions.select do |promotion| | ||
promotion.actions.any? { |promotion_action| promotion_action.discountable_class == Spree::Shipment } | ||
end | ||
shipment_discounter = ShipmentDiscounter.new(promotions: shipment_promotions) | ||
order.shipments.each { |shipment| shipment_discounter.call(shipment) } | ||
end | ||
|
||
def promotions | ||
@_promotions ||= begin | ||
preloader = ActiveRecord::Associations::Preloader.new | ||
(connected_order_promotions | sale_promotions).select do |promotion| | ||
promotion.activatable?(order) | ||
end.map do |promotion| | ||
preloader.preload(promotion.rules.select { |r| r.type == "Spree::Promotion::Rules::Product" }, :products) | ||
preloader.preload(promotion.rules.select { |r| r.type == "Spree::Promotion::Rules::Store" }, :stores) | ||
preloader.preload(promotion.rules.select { |r| r.type == "Spree::Promotion::Rules::Taxon" }, :taxons) | ||
preloader.preload(promotion.rules.select { |r| r.type == "Spree::Promotion::Rules::User" }, :users) | ||
preloader.preload(promotion.actions.select { |a| a.respond_to?(:calculator) }, :calculator) | ||
promotion | ||
end | ||
end.select { |promotion| promotion.order_discountable?(order) } | ||
end | ||
|
||
def connected_order_promotions | ||
order.promotions.includes(promotion_includes).select(&:active?) | ||
end | ||
|
||
def sale_promotions | ||
Spree::Promotion.where(apply_automatically: true).active.includes(promotion_includes) | ||
end | ||
|
||
def promotion_includes | ||
[ | ||
:promotion_rules, | ||
:promotion_actions, | ||
] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Discounts | ||
class ShipmentDiscounter | ||
attr_reader :promotions | ||
|
||
def initialize(promotions:) | ||
@promotions = promotions | ||
end | ||
|
||
def call(shipment) | ||
shipment.discounts = promotions.select do |promotion| | ||
promotion.shipment_discountable?(shipment) | ||
end.flat_map do |promotion| | ||
promotion.actions.select do |action| | ||
action.discountable_class == Spree::Shipment | ||
end.map do |action| | ||
action.discount(shipment) | ||
end | ||
end | ||
|
||
chosen_discounts = Spree::Config.discount_chooser_class.new(shipment).call(discounts) | ||
shipment.discounts = chosen_discounts | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters