How to use a custom promotion adjuster
In this guide, we'll see how to create a custom promotion adjuster to use our custom logic that handles promotion adjustments every time an order is updated.
Every time an order is updated, its eligible promotions might not be valid anymore; for example, this could happen if a promotion is tight to a specific order total threshold, and this value changes within the update. In the same way, changing the order could make it eligible for new promotions.
In Solidus, this behavior is encapsulated in the
Spree::Promotion::OrderAdjustmentsRecalculator
,
class, which is called every time the order is updated. This class iterates over all
existing promotion adjustments and recalculates their amount and eligibility.
Please, keep in mind that promotion adjustments are handled before taxes are calculated, such that taxes always respect promotions.
We'll now see how to use our own class instead of the one provided by Solidus.
First of all, let's declare which promotion adjuster class we want to use, with the corresponding configuration:
Spree.config do |config|
# ...
config.promotion_adjuster_class = 'MyStore::PromotionAdjuster'
end
Next, let's create our custom class, which responds to the same interface of the original promotion adjuster class provided by Solidus:
module MyStore
class PromotionAdjuster
def initialize(order)
@order = order
end
def call
# Your custom logic here.
end
end
end
Keep in mind that if you define your class as:
class PromotionAdjuster < ::Spree::Promotion::OrderAdjustmentsRecalculator
you will be able to use super
in its methods to execute parts of the
original class provided by Solidus.