From 4d6c77549c1ec6d43768d708ec2fde595241b0c9 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Fri, 27 Oct 2023 13:57:03 +0200 Subject: [PATCH 1/2] Add configuration endpoint: Coupon Handler --- api/app/controllers/spree/api/coupon_codes_controller.rb | 4 ++-- core/lib/spree/app_configuration.rb | 8 ++++++++ core/spec/lib/spree/app_configuration_spec.rb | 4 ++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/api/app/controllers/spree/api/coupon_codes_controller.rb b/api/app/controllers/spree/api/coupon_codes_controller.rb index 0fb7138943f..614ba9d77fe 100644 --- a/api/app/controllers/spree/api/coupon_codes_controller.rb +++ b/api/app/controllers/spree/api/coupon_codes_controller.rb @@ -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 @@ -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 diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index 49baffcc554..91b97f6b932 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -365,6 +365,14 @@ 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 your own Mailer for promotion code batch mailer. # # @!attribute [rw] promotion_code_batch_mailer_class diff --git a/core/spec/lib/spree/app_configuration_spec.rb b/core/spec/lib/spree/app_configuration_spec.rb index 46f83de4b69..d2c647d188e 100644 --- a/core/spec/lib/spree/app_configuration_spec.rb +++ b/core/spec/lib/spree/app_configuration_spec.rb @@ -36,6 +36,10 @@ 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 "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 From 1ceffb62edf32cd3e21fa166ffa3de9c33763b10 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Fri, 27 Oct 2023 14:22:43 +0200 Subject: [PATCH 2/2] Provide a configuration option for the shipping promotion handler Different promotion systems might do different things here. --- core/app/models/spree/order.rb | 2 +- core/lib/spree/app_configuration.rb | 8 ++++++++ core/spec/lib/spree/app_configuration_spec.rb | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index d7aa5dbab60..3712f21d9ef 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -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 diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index 91b97f6b932..cf03479e94d 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -373,6 +373,14 @@ def default_pricing_options # 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 diff --git a/core/spec/lib/spree/app_configuration_spec.rb b/core/spec/lib/spree/app_configuration_spec.rb index d2c647d188e..88454e46c00 100644 --- a/core/spec/lib/spree/app_configuration_spec.rb +++ b/core/spec/lib/spree/app_configuration_spec.rb @@ -40,6 +40,10 @@ 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