-
-
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
132 additions
and
17 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 LineItemUpdater | ||
attr_reader :promotions | ||
|
||
def initialize(promotions:) | ||
@promotions = promotions | ||
end | ||
|
||
def call(line_item) | ||
discounts = promotions.select do |promotion| | ||
promotion.line_item_eligible?(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
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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Spree | ||
module Discounts | ||
class ShipmentUpdater | ||
attr_reader :promotions | ||
|
||
def initialize(promotions:) | ||
@promotions = promotions | ||
end | ||
|
||
def call(shipment) | ||
shipment.discounts = promotions.select do |promotion| | ||
promotion.shipment_eligible?(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.promo_total = chosen_discounts.sum(&:amount) | ||
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