Skip to content

Commit

Permalink
Merge pull request #5466 from mamhoff/configurable-coupon-handler
Browse files Browse the repository at this point in the history
Configurable promotion handlers
  • Loading branch information
elia authored Oct 30, 2023
2 parents b653f69 + 1ceffb6 commit 34dd484
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions api/app/controllers/spree/api/coupon_codes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create
authorize! :update, @order, order_token

@order.coupon_code = params[:coupon_code]
@handler = PromotionHandler::Coupon.new(@order).apply
@handler = Spree::Config.coupon_code_handler_class.new(@order).apply

if @handler.successful?
render 'spree/api/promotions/handler', status: 200
Expand All @@ -24,7 +24,7 @@ def destroy
authorize! :update, @order, order_token

@order.coupon_code = params[:id]
@handler = PromotionHandler::Coupon.new(@order).remove
@handler = Spree::Config.coupon_code_handler_class.new(@order).remove

if @handler.successful?
render 'spree/api/promotions/handler', status: 200
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def create_shipments_for_line_item(line_item)
end

def apply_shipping_promotions
Spree::PromotionHandler::Shipping.new(self).activate
Spree::Config.shipping_promotion_handler_class.new(self).activate
recalculate
end

Expand Down
16 changes: 16 additions & 0 deletions core/lib/spree/app_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,22 @@ def default_pricing_options
# Spree::OrderUpdater.
class_name_attribute :order_recalculator_class, default: 'Spree::OrderUpdater'

# Allows providing a different coupon code handler.
# @!attribute [rw] coupon_code_handler_class
# @see Spree::PromotionHandler::Coupon
# @return [Class] an object that conforms to the API of
# the standard coupon code handler class
# Spree::PromotionHandler::Coupon.
class_name_attribute :coupon_code_handler_class, default: 'Spree::PromotionHandler::Coupon'

# Allows providing a different shipping promotion handler.
# @!attribute [rw] shipping_promotion_handler_class
# @see Spree::PromotionHandler::Shipping
# @return [Class] an object that conforms to the API of
# the standard shipping promotion handler class
# Spree::PromotionHandler::Coupon.
class_name_attribute :shipping_promotion_handler_class, default: 'Spree::PromotionHandler::Shipping'

# Allows providing your own Mailer for promotion code batch mailer.
#
# @!attribute [rw] promotion_code_batch_mailer_class
Expand Down
8 changes: 8 additions & 0 deletions core/spec/lib/spree/app_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
expect(prefs.promotion_adjuster_class).to eq Spree::Promotion::OrderAdjustmentsRecalculator
end

it "uses promotion handler coupon class by default" do
expect(prefs.coupon_code_handler_class).to eq Spree::PromotionHandler::Coupon
end

it "uses promotion handler shipping class by default" do
expect(prefs.shipping_promotion_handler_class).to eq Spree::PromotionHandler::Shipping
end

it "has a getter for the pricing options class provided by the variant price selector class" do
expect(prefs.pricing_options_class).to eq Spree::Variant::PriceSelector.pricing_options_class
end
Expand Down

0 comments on commit 34dd484

Please sign in to comment.