-
-
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.
Merge pull request #5805 from mamhoff/import-solidus-friendly-promotions
Introduce SolidusPromotions
- Loading branch information
Showing
322 changed files
with
15,746 additions
and
2 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
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
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,10 @@ | ||
{ | ||
"extends": "eslint:recommended", | ||
"parserOptions": { | ||
"ecmaVersion": 2023, | ||
"sourceType": "module" | ||
}, | ||
"globals": { | ||
"Turbo": "readonly" | ||
} | ||
} |
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 @@ | ||
_extends: .github |
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,150 @@ | ||
# Migrating from Solidus' promotion system to SolidusPromotions | ||
|
||
The system is designed to completely replace the Solidus promotion system. Follow these steps to migrate your store to the gem: | ||
|
||
## Install solidus_promotions | ||
|
||
Add the following line to your `Gemfile`: | ||
|
||
```rb | ||
gem "solidus_promotions" | ||
``` | ||
|
||
Then run | ||
|
||
```sh | ||
bundle install | ||
bundle exec rails generate solidus_promotions:install | ||
``` | ||
|
||
This will install the extension. It will add new tables, and new routes. It will also generate an initializer in `config/initializers/solidus_promotions.rb`. | ||
|
||
For the time being, comment out the following lines: | ||
|
||
```rb | ||
# Make sure we use Spree::SimpleOrderContents | ||
# Spree::Config.order_contents_class = "Spree::SimpleOrderContents" | ||
# Set the promotion configuration to ours | ||
# Spree::Config.promotions = SolidusPromotions.configuration | ||
``` | ||
|
||
This makes sure that the behavior of the current promotion system does not change - yet. | ||
|
||
## Migrate existing promotions | ||
|
||
Now, run the promotion migrator: | ||
|
||
```sh | ||
bundle exec rails solidus_promotions:migrate_existing_promotions | ||
``` | ||
|
||
This will create equivalents of the legacy promotion configuration in SolidusPromotions. | ||
|
||
Now, change `config/initializers/solidus_promotions.rb` to use your new promotion configuration: | ||
|
||
## Change store behavior to use SolidusPromotions | ||
|
||
```rb | ||
# Make sure we use Spree::SimpleOrderContents | ||
Spree::Config.order_contents_class = "Spree::SimpleOrderContents" | ||
# Set the promotion configuration to ours | ||
Spree::Config.promotions = SolidusPromotions.configuration | ||
|
||
# Sync legacy order promotions with the new promotion system | ||
SolidusPromotions.config.sync_order_promotions = true | ||
``` | ||
|
||
From a user's perspective, your promotions should work as before. | ||
|
||
Before you create new promotions, migrate the adjustments and order promotions in your database: | ||
|
||
```rb | ||
bundle exec rails solidus_promotions:migrate_adjustments:up | ||
bundle exec rails solidus_promotions:migrate_order_promotions:up | ||
|
||
``` | ||
|
||
Check your `spree_adjustments` table for correctness. If things went wrong, you should be able to roll back with | ||
|
||
```rb | ||
bundle exec rails solidus_promotions:migrate_adjustments:down | ||
bundle exec rails solidus_promotions:migrate_order_promotions:down | ||
``` | ||
|
||
Both of these tasks only work if every promotion rule and promotion action have an equivalent condition or benefit in SolidusFrienndlyPromotions. Benefits are connected to their originals promotion action using the `SolidusPromotions#original_promotion_action_id`, Promotions are connected to their originals using the `SolidusPromotions#original_promotion_id`. | ||
|
||
Once these tasks have run and everything works, you can stop syncing legacy order promotions and new order promotions: | ||
|
||
```rb | ||
SolidusPromotions.config.sync_order_promotions = false | ||
``` | ||
|
||
## Solidus Starter Frontend (and other custom frontends) | ||
|
||
Stores that have a custom coupon codes controller, such as Solidus' starter frontend, have to change the coupon promotion handler to the one from this gem. Cange any reference to `Spree::PromotionHandler::Coupon` to `Spree::Config.promotions.coupon_code_handler_class`. | ||
|
||
## Migrating custom rules and actions | ||
|
||
If you have custom promotion rules or actions, you need to create new conditions and benefits, respectively. | ||
|
||
> [!IMPORTANT] | ||
> SolidusPromotions currently only supports actions that discount line items and shipments, as well as creating discounted line items. If you have actions that create order-level adjustments, we currently have no support for that. | ||
In our experience, using the three actions can do almost all the things necessary, since they are customizable using calculators. | ||
|
||
Rules share a lot of the previous API. If you make use of `#actionable?`, you might want to migrate your rule to be a line-item level rule: | ||
|
||
```rb | ||
class MyRule < Spree::PromotionRule | ||
def actionable?(promotable) | ||
promotable.quantity > 3 | ||
end | ||
end | ||
``` | ||
|
||
would become: | ||
|
||
```rb | ||
class MyCondition < SolidusPromotions::Condition | ||
include LineItemLevelCondition | ||
|
||
def eligible?(promotable) | ||
promotable.quantity > 3 | ||
end | ||
end | ||
``` | ||
|
||
Now, create your own Promotion conversion map: | ||
|
||
```rb | ||
require 'solidus_promotions/promotion_map' | ||
|
||
MY_PROMOTION_MAP = SolidusPromotions::PROMOTION_MAP.deep_merge( | ||
rules: { | ||
MyRule => MyCondition | ||
} | ||
) | ||
``` | ||
|
||
The value of the conversion map can also be a callable that takes the original promotion rule and should return a new condition. | ||
|
||
```rb | ||
require 'solidus_promotions/promotion_map' | ||
|
||
MY_PROMOTION_MAP = SolidusPromotions::PROMOTION_MAP.deep_merge( | ||
rules: { | ||
MyRule => ->(old_promotion_rule) { | ||
MyCondition.new(preferred_quantity: old_promotion_rule.preferred_count) | ||
} | ||
} | ||
) | ||
``` | ||
|
||
You can now run our migrator with your map: | ||
|
||
```rb | ||
require 'solidus_promotions/promotion_migrator' | ||
require 'my_promotion_map' | ||
|
||
SolidusPromotions::PromotionMigrator.new(MY_PROMOTION_MAP).call | ||
``` |
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,122 @@ | ||
# Solidus Promotions | ||
|
||
This gem contains Solidus' recommended promotion system. It is slated to replace the promotion system in the `legacy_promotions` gem. | ||
|
||
The basic architecture is very similar to the legacy promotion system, but with a few decisive tweaks, which I'll explain in the coming sections. | ||
|
||
## Architecture | ||
|
||
This extension centralizes promotion handling in the order updater. A service class, the `SolidusPromotions::OrderAdjuster` applies the current promotion configuration to the order, adjusting or removing adjustments as necessary. | ||
|
||
`SolidusPromotions::Promotion` objects have benefits, and benefits have conditions. For example, a promotion that is "20% off shirts" would have a benefit of type "AdjustLineItem", and that benefit would have a condition of type "LineItemTaxon" that makes sure only line items with the "shirts" taxon will get the benefit. | ||
|
||
### Promotion lanes | ||
|
||
Promotions get applied by "lane". Promotions within a lane conflict with each other, whereas promotions that do not share a lane will apply sequentially in the order of the lanes. By default these are "pre", "default" and "post", but you can configure this using the SolidusPromotions initializer: | ||
|
||
```rb | ||
SolidusPromotions.configure do |config| | ||
config.preferred_lanes = { | ||
pre: 0, | ||
default: 1, | ||
grogu: 2, | ||
post: 3 | ||
} | ||
end | ||
``` | ||
|
||
### Benefits | ||
|
||
Solidus Friendly Promotions ships with only three benefit types by default that should cover most use cases: `AdjustLineItem`, `AdjustShipment` and `CreateDiscountedItem`. There is no benefit that creates order-level adjustments, as this feature of Solidus' legacy promotions system has proven to be very difficult for customer service and finance departments due to the difficulty of accruing order-level adjustments to individual line items when e.g. processing returns. In order to give a fixed discount to all line items in an order, use the `AdjustLineItem` benefit with the `DistributedAmount` calculator. | ||
|
||
Alle benefits are calculable. By setting their `calculator` to one of the classes provided, a great range of discounts is possible. | ||
|
||
#### `AdjustLineItem` | ||
|
||
Benefits of this class will create promotion adjustments on line items. By default, they will create a discount on every line item in the order. If you want to restrict which line items get the discount, add line-item level conditions, such as `LineItemProduct`. | ||
|
||
#### `AdjustShipment` | ||
|
||
Benefits of this class will create promotion adjustments on shipments. By default, they will create a discount on every shipment in the order. If you want to restrict which shipments get a discount, add shipment-level conditions, such as `ShippingMethod`. | ||
|
||
### Conditions | ||
|
||
Every type of benefit has a list of rules that can be applied to them. When calculating adjustments for an order, benefits will only produce adjustments on line items or shipments if all their respective conditions are true. | ||
|
||
### Connecting promotions to orders | ||
|
||
When there is a join record `SolidusPromotions::OrderPromotion`, the promotion and the order will be "connected", and the promotion will be applied even if it does not `apply_automatically` (see below). This is different from Solidus' legacy promotion system here in that promotions are not automatically connected to orders when they produce an adjustment. | ||
|
||
If you want to create an `OrderPromotion` record, the usual way to do this is via a promotion handler: | ||
|
||
- `SolidusPromotions::PromotionHandler::Coupon`: Connects orders to promotions if a customer or service agent enters a matching promotion code. | ||
- `SolidusPromotions::PromotionHandler::Page`: Connects orders to promotions if a customer visits a page with the correct path. This handler is not integrated in core Solidus, and must be integrated by you. | ||
- `SolidusPromotions::PromotionHandler::Page`: Connects orders to promotions if a customer visits a page with the correct path. This handler is not integrated in core Solidus, and must be integrated by you. | ||
|
||
### Promotion categories | ||
|
||
Promotion categories simply allow admins to group promotions. They have no further significance with regards to the functionality of the promotion system. | ||
|
||
### Promotion recalculation | ||
|
||
Solidus allows changing orders up until when they are shipped. SolidusPromotions therefore will recalculate orders up until when they are shipped as well. If your admins change promotions rather than add new ones and carefully manage the start and end dates of promotions, you might want to disable this feature: | ||
|
||
```rb | ||
SolidusPromotions.configure do |config| | ||
config.recalculate_complete_orders = false | ||
end | ||
``` | ||
|
||
## Installation | ||
|
||
Add solidus_promotions to your Gemfile: | ||
|
||
```ruby | ||
gem 'solidus_promotions' | ||
``` | ||
|
||
Once this project is out of the research phase, a proper gem release will follow. | ||
|
||
Bundle your dependencies and run the installation generator: | ||
|
||
```shell | ||
bin/rails generate solidus_promotions:install | ||
``` | ||
|
||
This will create the tables for this extension. It will also replace the promotion administration system under | ||
`/admin/promotions` with a new one that needs `turbo-rails`. It will also create an initializer within which Solidus is configured to use `Spree::SimpleOrderContents` and this extension's `OrderAdjuster` classes. Feel free to override with your own implementations! | ||
|
||
## Usage | ||
|
||
Add a promotion using the admin. Add benefits with conditions, and observe promotions getting applied how you'd expect them to. | ||
|
||
In the admin screen, you can set a number of attributes on your promotion: | ||
- Name: The name of the promotion. The `name` attribute will also be used to generate adjustment labels. In multi-lingual stores, you probably want different promotions per language for this reason. | ||
|
||
- Description: This is purely informative. Some stores use `description` in order display information about this promotion to customers, but there is nothing in core Solidus that does it. | ||
|
||
- Start and End: Outside of the time range between `starts_at` and `expires_at`, the promotion will not be eligible to any order. | ||
|
||
- Usage Limit: `usage_limit` controls to how many orders this promotion can be applied, independent of promotion code or user. This is most commonly used to limit the risk of losing too much revenue with a particular promotion. | ||
|
||
- Path: `path` is a URL path that connects the promotion to the order upon visitation. Not currently implemented in either Solidus core or this extension. | ||
|
||
- Per Code Usage Limit: How often each code can be used. Useful for limiting how many orders can be placed with a single promotion code. | ||
|
||
- Apply Automatically: Whether this promotion should apply automatically. This means that the promotion is checked for eligibility every time the customer's order is recalculated. Promotion Codes and automatic applications are incompatible. | ||
|
||
- Promotion Category: Used to group promotions in the admin view. | ||
|
||
## Development | ||
|
||
When testing your application's integration with this extension you may use its factories. | ||
You can load Solidus core factories along with this extension's factories using this statement: | ||
|
||
```ruby | ||
SolidusDevSupport::TestingSupport::Factories.load_for(SolidusPromotions::Engine) | ||
``` | ||
|
||
|
||
## License | ||
|
||
Copyright (c) 2024 Martin Meyerhoff, Solidus Team, released under the New BSD License. |
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rubygems" | ||
require "rake" | ||
require "rake/testtask" | ||
require "rspec/core/rake_task" | ||
require "solidus_legacy_promotions" | ||
require "spree/testing_support/dummy_app/rake_tasks" | ||
require "solidus_admin/testing_support/dummy_app/rake_tasks" | ||
require "bundler/gem_tasks" | ||
|
||
RSpec::Core::RakeTask.new | ||
task default: :spec | ||
|
||
DummyApp::RakeTasks.new( | ||
gem_root: File.dirname(__FILE__), | ||
lib_name: "solidus_promotions" | ||
) | ||
|
||
task test_app: "db:reset" |
13 changes: 13 additions & 0 deletions
13
promotions/app/assets/config/solidus_promotions/manifest.js
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,13 @@ | ||
//= link backend/solidus_promotions.js | ||
//= link backend/solidus_promotions/controllers/application.js | ||
//= link backend/solidus_promotions/controllers/index.js | ||
//= link backend/solidus_promotions/controllers/calculator_tiers_controller.js | ||
//= link backend/solidus_promotions/controllers/flash_controller.js | ||
//= link backend/solidus_promotions/controllers/product_option_values_controller.js | ||
//= link backend/solidus_promotions/web_components/option_value_picker.js | ||
//= link backend/solidus_promotions/web_components/product_picker.js | ||
//= link backend/solidus_promotions/web_components/user_picker.js | ||
//= link backend/solidus_promotions/web_components/taxon_picker.js | ||
//= link backend/solidus_promotions/web_components/variant_picker.js | ||
//= link backend/solidus_promotions/web_components/number_with_currency.js | ||
//= link backend/solidus_promotions/web_components/select_two.js |
11 changes: 11 additions & 0 deletions
11
promotions/app/decorators/models/solidus_promotions/adjustment_decorator.rb
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusPromotions | ||
module AdjustmentDecorator | ||
def self.prepended(base) | ||
base.scope :solidus_promotion, -> { where(source_type: "SolidusPromotions::Benefit") } | ||
end | ||
|
||
Spree::Adjustment.prepend self | ||
end | ||
end |
Oops, something went wrong.