solidus_friendly_promotions 1.0.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +61 -0
- data/.gem_release.yml +5 -0
- data/.github/stale.yml +1 -0
- data/.github_changelog_generator +2 -0
- data/.gitignore +21 -0
- data/.rspec +2 -0
- data/.rubocop.yml +29 -0
- data/.rubocop_todo.yml +157 -0
- data/.standard.yml +4 -0
- data/CHANGELOG.md +1 -0
- data/Gemfile +42 -0
- data/LICENSE +26 -0
- data/MIGRATING.md +134 -0
- data/README.md +144 -0
- data/Rakefile +7 -0
- data/app/assets/config/solidus_friendly_promotions/manifest.js +7 -0
- data/app/controllers/solidus_friendly_promotions/admin/base_controller.rb +54 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_actions_controller.rb +103 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_categories_controller.rb +13 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_code_batches_controller.rb +30 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_codes_controller.rb +58 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotion_rules_controller.rb +105 -0
- data/app/controllers/solidus_friendly_promotions/admin/promotions_controller.rb +64 -0
- data/app/decorators/models/solidus_friendly_promotions/adjustment_decorator.rb +26 -0
- data/app/decorators/models/solidus_friendly_promotions/line_item_decorator.rb +7 -0
- data/app/decorators/models/solidus_friendly_promotions/order_decorator.rb +42 -0
- data/app/decorators/models/solidus_friendly_promotions/order_recalculator_decorator.rb +13 -0
- data/app/decorators/models/solidus_friendly_promotions/shipment_decorator.rb +14 -0
- data/app/decorators/models/solidus_friendly_promotions/shipping_rate_decorator.rb +29 -0
- data/app/helpers/solidus_friendly_promotions/admin/promotion_actions_helper.rb +23 -0
- data/app/helpers/solidus_friendly_promotions/admin/promotion_rules_helper.rb +25 -0
- data/app/javascript/solidus_friendly_promotions/controllers/application.js +9 -0
- data/app/javascript/solidus_friendly_promotions/controllers/calculator_tiers_controller.js +37 -0
- data/app/javascript/solidus_friendly_promotions/controllers/flash_controller.js +10 -0
- data/app/javascript/solidus_friendly_promotions/controllers/index.js +8 -0
- data/app/javascript/solidus_friendly_promotions/controllers/product_option_values_controller.js +62 -0
- data/app/javascript/solidus_friendly_promotions/jquery/option_value_picker.js +44 -0
- data/app/javascript/solidus_friendly_promotions.js +12 -0
- data/app/jobs/solidus_friendly_promotions/promotion_code_batch_job.rb +26 -0
- data/app/mailers/solidus_friendly_promotions/promotion_code_batch_mailer.rb +15 -0
- data/app/models/concerns/solidus_friendly_promotions/discountable_amount.rb +21 -0
- data/app/models/concerns/solidus_friendly_promotions/rules/line_item_applicable_order_rule.rb +23 -0
- data/app/models/concerns/solidus_friendly_promotions/rules/line_item_level_rule.rb +15 -0
- data/app/models/concerns/solidus_friendly_promotions/rules/order_level_rule.rb +15 -0
- data/app/models/concerns/solidus_friendly_promotions/rules/shipment_level_rule.rb +15 -0
- data/app/models/solidus_friendly_promotions/actions/adjust_line_item.rb +15 -0
- data/app/models/solidus_friendly_promotions/actions/adjust_line_item_quantity_groups.rb +116 -0
- data/app/models/solidus_friendly_promotions/actions/adjust_shipment.rb +15 -0
- data/app/models/solidus_friendly_promotions/calculators/distributed_amount.rb +37 -0
- data/app/models/solidus_friendly_promotions/calculators/flat_rate.rb +21 -0
- data/app/models/solidus_friendly_promotions/calculators/flexi_rate.rb +24 -0
- data/app/models/solidus_friendly_promotions/calculators/percent.rb +17 -0
- data/app/models/solidus_friendly_promotions/calculators/tiered_flat_rate.rb +56 -0
- data/app/models/solidus_friendly_promotions/calculators/tiered_percent.rb +66 -0
- data/app/models/solidus_friendly_promotions/distributed_amounts_handler.rb +43 -0
- data/app/models/solidus_friendly_promotions/eligibility_result.rb +5 -0
- data/app/models/solidus_friendly_promotions/eligibility_results.rb +48 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/choose_discounts.rb +21 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/discount_order.rb +76 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/load_promotions.rb +54 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster/persist_discounted_order.rb +81 -0
- data/app/models/solidus_friendly_promotions/friendly_promotion_adjuster.rb +25 -0
- data/app/models/solidus_friendly_promotions/item_discount.rb +21 -0
- data/app/models/solidus_friendly_promotions/migration_support/order_promotion_syncer.rb +54 -0
- data/app/models/solidus_friendly_promotions/order_promotion.rb +23 -0
- data/app/models/solidus_friendly_promotions/permission_sets/friendly_promotion_management.rb +15 -0
- data/app/models/solidus_friendly_promotions/products_promotion_rule.rb +8 -0
- data/app/models/solidus_friendly_promotions/promotion.rb +166 -0
- data/app/models/solidus_friendly_promotions/promotion_action.rb +73 -0
- data/app/models/solidus_friendly_promotions/promotion_category.rb +9 -0
- data/app/models/solidus_friendly_promotions/promotion_code/batch_builder.rb +72 -0
- data/app/models/solidus_friendly_promotions/promotion_code.rb +55 -0
- data/app/models/solidus_friendly_promotions/promotion_code_batch.rb +25 -0
- data/app/models/solidus_friendly_promotions/promotion_handler/coupon.rb +113 -0
- data/app/models/solidus_friendly_promotions/promotion_handler/page.rb +30 -0
- data/app/models/solidus_friendly_promotions/promotion_rule.rb +55 -0
- data/app/models/solidus_friendly_promotions/promotion_rules_store.rb +8 -0
- data/app/models/solidus_friendly_promotions/promotion_rules_taxon.rb +8 -0
- data/app/models/solidus_friendly_promotions/promotion_rules_user.rb +8 -0
- data/app/models/solidus_friendly_promotions/rules/discounted_item_total.rb +22 -0
- data/app/models/solidus_friendly_promotions/rules/first_order.rb +31 -0
- data/app/models/solidus_friendly_promotions/rules/first_repeat_purchase_since.rb +31 -0
- data/app/models/solidus_friendly_promotions/rules/item_total.rb +86 -0
- data/app/models/solidus_friendly_promotions/rules/line_item_option_value.rb +37 -0
- data/app/models/solidus_friendly_promotions/rules/line_item_product.rb +52 -0
- data/app/models/solidus_friendly_promotions/rules/line_item_taxon.rb +55 -0
- data/app/models/solidus_friendly_promotions/rules/minimum_quantity.rb +48 -0
- data/app/models/solidus_friendly_promotions/rules/nth_order.rb +40 -0
- data/app/models/solidus_friendly_promotions/rules/one_use_per_user.rb +25 -0
- data/app/models/solidus_friendly_promotions/rules/option_value.rb +28 -0
- data/app/models/solidus_friendly_promotions/rules/product.rb +85 -0
- data/app/models/solidus_friendly_promotions/rules/shipping_method.rb +19 -0
- data/app/models/solidus_friendly_promotions/rules/store.rb +26 -0
- data/app/models/solidus_friendly_promotions/rules/taxon.rb +98 -0
- data/app/models/solidus_friendly_promotions/rules/user.rb +35 -0
- data/app/models/solidus_friendly_promotions/rules/user_logged_in.rb +16 -0
- data/app/models/solidus_friendly_promotions/rules/user_role.rb +42 -0
- data/app/models/solidus_friendly_promotions/shipping_rate_discount.rb +11 -0
- data/app/models/solidus_friendly_promotions/simple_order_contents.rb +27 -0
- data/app/models/solidus_friendly_promotions.rb +7 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/_calculator_select.html.erb +16 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/_form.html.erb +3 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/_promotion_action.html.erb +29 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/_type_select.html.erb +14 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_adjust_line_item.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_adjust_line_item_quantity_groups.html.erb +13 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_adjust_shipment.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_calculator_fields.erb +8 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/_default_fields.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/distributed_amount/_fields.html.erb +56 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/flat_rate/_fields.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/tiered_flat_rate/_fields.html.erb +34 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/tiered_flat_rate/_tier_fields.html.erb +32 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/tiered_percent/_fields.html.erb +34 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/calculators/tiered_percent/_tier_fields.html.erb +32 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/edit.html.erb +23 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_actions/new.html.erb +26 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_categories/_form.html.erb +14 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_categories/edit.html.erb +10 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_categories/index.html.erb +47 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_categories/new.html.erb +10 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_codes/index.csv.ruby +8 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_codes/index.html.erb +32 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_codes/new.html.erb +31 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/_promotion_rule.html.erb +22 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/_type_select.html.erb +20 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/new.html.erb +24 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_first_order.html.erb +3 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_first_repeat_purchase_since.html.erb +6 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_item_total.html.erb +17 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_line_item_option_value.html.erb +25 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_line_item_product.html.erb +21 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_line_item_taxon.html.erb +17 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_minimum_quantity.html.erb +5 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_nth_order.html.erb +15 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_one_use_per_user.html.erb +3 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_option_value.html.erb +63 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_product.html.erb +44 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_shipping_method.html.erb +10 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_store.html.erb +9 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_taxon.html.erb +44 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_user.html.erb +7 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_user_logged_in.html.erb +3 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/_user_role.html.erb +15 -0
- data/app/views/solidus_friendly_promotions/admin/promotion_rules/rules/line_item_option_value/_option_value_fields.html.erb +17 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/_activations_edit.html.erb +22 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/_activations_new.html.erb +43 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/_form.html.erb +96 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/edit.html.erb +73 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/index.html.erb +124 -0
- data/app/views/solidus_friendly_promotions/admin/promotions/new.html.erb +9 -0
- data/app/views/solidus_friendly_promotions/admin/shared/_promotion_sub_menu.html.erb +14 -0
- data/app/views/solidus_friendly_promotions/promotion_code_batch_mailer/promotion_code_batch_errored.text.erb +2 -0
- data/app/views/solidus_friendly_promotions/promotion_code_batch_mailer/promotion_code_batch_finished.text.erb +2 -0
- data/bin/console +17 -0
- data/bin/importmap +4 -0
- data/bin/rails +7 -0
- data/bin/rails-engine +13 -0
- data/bin/rails-sandbox +16 -0
- data/bin/rake +7 -0
- data/bin/sandbox +75 -0
- data/bin/setup +8 -0
- data/config/importmap.rb +12 -0
- data/config/initializers/solidus_friendly_promotions.rb +3 -0
- data/config/locales/en.yml +255 -0
- data/config/routes.rb +18 -0
- data/db/migrate/20230703101637_create_promotions.rb +16 -0
- data/db/migrate/20230703141116_create_promotion_categories.rb +14 -0
- data/db/migrate/20230703143943_create_promotion_rules.rb +12 -0
- data/db/migrate/20230704083830_add_rule_tables.rb +31 -0
- data/db/migrate/20230704093625_create_promotion_actions.rb +14 -0
- data/db/migrate/20230704102444_create_promotion_codes.rb +11 -0
- data/db/migrate/20230704102656_create_promotion_code_batches.rb +33 -0
- data/db/migrate/20230705171556_create_friendly_order_promotions.rb +11 -0
- data/db/migrate/20230725074235_create_shipping_rate_discounts.rb +12 -0
- data/db/migrate/20230928093138_add_lane_to_solidus_friendly_promotions_promotions.rb +5 -0
- data/db/migrate/20231006134042_add_customer_label_to_promotions.rb +7 -0
- data/db/migrate/20231011100059_add_db_comments_to_friendly_order_promotions.rb +61 -0
- data/db/migrate/20231011120928_add_db_comments_to_friendly_promotion_rules_taxons.rb +54 -0
- data/db/migrate/20231011131324_add_db_comments_to_friendly_promotion_rules.rb +60 -0
- data/db/migrate/20231011142040_add_db_comments_to_friendly_promotion_rules_users.rb +53 -0
- data/db/migrate/20231011154553_allow_null_promotion_ids.rb +9 -0
- data/db/migrate/20231011155822_add_db_comments_to_friendly_promotions.rb +123 -0
- data/db/migrate/20231011163030_add_db_comments_to_friendly_promotion_codes.rb +60 -0
- data/db/migrate/20231011173312_add_db_comments_to_friendly_promotion_code_batches.rb +91 -0
- data/db/migrate/20231011184205_add_db_comments_to_friendly_promotion_rules_stores.rb +53 -0
- data/db/migrate/20231011190222_add_db_comments_to_friendly_promotion_actions.rb +68 -0
- data/db/migrate/20231012020928_add_db_comments_to_friendly_products_promotion_rules.rb +52 -0
- data/db/migrate/20231012120928_add_db_comments_to_friendly_promotion_categories.rb +52 -0
- data/db/migrate/20231013181921_add_original_promotion_ids.rb +6 -0
- data/lib/generators/solidus_friendly_promotions/install/install_generator.rb +38 -0
- data/lib/generators/solidus_friendly_promotions/install/templates/initializer.rb +118 -0
- data/lib/solidus_friendly_promotions/configuration.rb +52 -0
- data/lib/solidus_friendly_promotions/engine.rb +37 -0
- data/lib/solidus_friendly_promotions/migrate_adjustments.rb +62 -0
- data/lib/solidus_friendly_promotions/migrate_order_promotions.rb +73 -0
- data/lib/solidus_friendly_promotions/nested_class_set.rb +24 -0
- data/lib/solidus_friendly_promotions/promotion_map.rb +96 -0
- data/lib/solidus_friendly_promotions/promotion_migrator.rb +103 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_order_factory.rb +20 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_order_promotion_factory.rb +8 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_promotion_category_factory.rb +7 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_promotion_code_factory.rb +8 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_promotion_factory.rb +91 -0
- data/lib/solidus_friendly_promotions/testing_support/factories/friendly_shipping_rate_discount_factory.rb +14 -0
- data/lib/solidus_friendly_promotions/testing_support.rb +9 -0
- data/lib/solidus_friendly_promotions/version.rb +5 -0
- data/lib/solidus_friendly_promotions.rb +18 -0
- data/lib/tasks/solidus_friendly_promotions/migrate_adjustments.rake +17 -0
- data/lib/tasks/solidus_friendly_promotions/migrate_existing_promotions.rake +12 -0
- data/lib/tasks/solidus_friendly_promotions/migrate_order_promotions.rake +17 -0
- data/solidus_friendly_promotions.gemspec +40 -0
- metadata +392 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
# A rule to apply a promotion only to line items with or without a chosen product
|
6
|
+
class LineItemProduct < PromotionRule
|
7
|
+
include LineItemLevelRule
|
8
|
+
|
9
|
+
MATCH_POLICIES = %w[include exclude].freeze
|
10
|
+
|
11
|
+
has_many :product_promotion_rules,
|
12
|
+
dependent: :destroy,
|
13
|
+
foreign_key: :promotion_rule_id,
|
14
|
+
class_name: "SolidusFriendlyPromotions::ProductsPromotionRule"
|
15
|
+
has_many :products,
|
16
|
+
class_name: "Spree::Product",
|
17
|
+
through: :product_promotion_rules
|
18
|
+
|
19
|
+
preference :match_policy, :string, default: MATCH_POLICIES.first
|
20
|
+
|
21
|
+
def eligible?(line_item, _options = {})
|
22
|
+
order_includes_product = product_ids.include?(line_item.variant.product_id)
|
23
|
+
success = inverse? ? !order_includes_product : order_includes_product
|
24
|
+
|
25
|
+
unless success
|
26
|
+
message_code = inverse? ? :has_excluded_product : :no_applicable_products
|
27
|
+
eligibility_errors.add(
|
28
|
+
:base,
|
29
|
+
eligibility_error_message(message_code),
|
30
|
+
error_code: message_code
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
success
|
35
|
+
end
|
36
|
+
|
37
|
+
def product_ids_string
|
38
|
+
product_ids.join(",")
|
39
|
+
end
|
40
|
+
|
41
|
+
def product_ids_string=(product_ids)
|
42
|
+
self.product_ids = product_ids.to_s.split(",").map(&:strip)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def inverse?
|
48
|
+
preferred_match_policy == "exclude"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class LineItemTaxon < PromotionRule
|
6
|
+
include LineItemLevelRule
|
7
|
+
|
8
|
+
has_many :promotion_rule_taxons, class_name: "SolidusFriendlyPromotions::PromotionRulesTaxon", foreign_key: :promotion_rule_id,
|
9
|
+
dependent: :destroy
|
10
|
+
has_many :taxons, through: :promotion_rule_taxons, class_name: "Spree::Taxon"
|
11
|
+
|
12
|
+
MATCH_POLICIES = %w[include exclude].freeze
|
13
|
+
|
14
|
+
validates :preferred_match_policy, inclusion: {in: MATCH_POLICIES}
|
15
|
+
|
16
|
+
preference :match_policy, :string, default: MATCH_POLICIES.first
|
17
|
+
|
18
|
+
def eligible?(line_item, _options = {})
|
19
|
+
found = Spree::Classification.where(
|
20
|
+
product_id: line_item.variant.product_id,
|
21
|
+
taxon_id: rule_taxon_ids_with_children
|
22
|
+
).exists?
|
23
|
+
|
24
|
+
case preferred_match_policy
|
25
|
+
when "include"
|
26
|
+
found
|
27
|
+
when "exclude"
|
28
|
+
!found
|
29
|
+
else
|
30
|
+
raise "unexpected match policy: #{preferred_match_policy.inspect}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def taxon_ids_string
|
35
|
+
taxons.pluck(:id).join(",")
|
36
|
+
end
|
37
|
+
|
38
|
+
def taxon_ids_string=(taxon_ids)
|
39
|
+
taxon_ids = taxon_ids.to_s.split(",").map(&:strip)
|
40
|
+
self.taxons = Spree::Taxon.find(taxon_ids)
|
41
|
+
end
|
42
|
+
|
43
|
+
def updateable?
|
44
|
+
true
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
# ids of taxons rules and taxons rules children
|
50
|
+
def rule_taxon_ids_with_children
|
51
|
+
taxons.flat_map { |taxon| taxon.self_and_descendants.ids }.uniq
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
# Promotion rule for ensuring an order contains a minimum quantity of
|
6
|
+
# applicable items.
|
7
|
+
#
|
8
|
+
# This promotion rule is only compatible with the "all" match policy. It
|
9
|
+
# doesn't make a lot of sense to use it without that policy as it reduces
|
10
|
+
# it to a simple quantity check across the entire order which would be
|
11
|
+
# better served by an item total rule.
|
12
|
+
class MinimumQuantity < PromotionRule
|
13
|
+
include OrderLevelRule
|
14
|
+
|
15
|
+
validates :preferred_minimum_quantity, numericality: {only_integer: true, greater_than: 0}
|
16
|
+
|
17
|
+
preference :minimum_quantity, :integer, default: 1
|
18
|
+
|
19
|
+
# Will look at all of the "applicable" line items in the order and
|
20
|
+
# determine if the sum of their quantity is greater than the minimum.
|
21
|
+
#
|
22
|
+
# "Applicable" items are ones that pass all eligibility checks of applicable rules.
|
23
|
+
#
|
24
|
+
# When false is returned, the reason will be included in the
|
25
|
+
# `eligibility_errors` object.
|
26
|
+
#
|
27
|
+
# @param order [Spree::Order] the order we want to check eligibility on
|
28
|
+
# @return [Boolean] true if promotion is eligible, false otherwise
|
29
|
+
def eligible?(order)
|
30
|
+
applicable_line_items = order.line_items.select do |line_item|
|
31
|
+
promotion.rules.select do |rule|
|
32
|
+
rule.applicable?(line_item)
|
33
|
+
end.all? { _1.eligible?(line_item) }
|
34
|
+
end
|
35
|
+
|
36
|
+
if applicable_line_items.sum(&:quantity) < preferred_minimum_quantity
|
37
|
+
eligibility_errors.add(
|
38
|
+
:base,
|
39
|
+
eligibility_error_message(:quantity_less_than_minimum, count: preferred_minimum_quantity),
|
40
|
+
error_code: :quantity_less_than_minimum
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
eligibility_errors.empty?
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class NthOrder < PromotionRule
|
6
|
+
include OrderLevelRule
|
7
|
+
|
8
|
+
preference :nth_order, :integer, default: 2
|
9
|
+
# It does not make sense to have this apply to the first order using preferred_nth_order == 1
|
10
|
+
# Instead we could use the first_order rule
|
11
|
+
validates :preferred_nth_order, numericality: {only_integer: true, greater_than: 1}
|
12
|
+
|
13
|
+
# This is never eligible if the order does not have a user, and that user does not have any previous completed orders.
|
14
|
+
#
|
15
|
+
# Use the first order rule if you want a promotion to be applied to the first order for a user.
|
16
|
+
# @param order [Spree::Order]
|
17
|
+
def eligible?(order, _options = {})
|
18
|
+
return false unless order.user
|
19
|
+
|
20
|
+
nth_order?(order)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def completed_order_count(order)
|
26
|
+
order
|
27
|
+
.user
|
28
|
+
.orders
|
29
|
+
.complete
|
30
|
+
.where(Spree::Order.arel_table[:completed_at].lt(order.completed_at || Time.current))
|
31
|
+
.count
|
32
|
+
end
|
33
|
+
|
34
|
+
def nth_order?(order)
|
35
|
+
count = completed_order_count(order) + 1
|
36
|
+
count == preferred_nth_order
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class OneUsePerUser < PromotionRule
|
6
|
+
include OrderLevelRule
|
7
|
+
|
8
|
+
def eligible?(order, _options = {})
|
9
|
+
if order.user.present?
|
10
|
+
if promotion.used_by?(order.user, [order])
|
11
|
+
eligibility_errors.add(
|
12
|
+
:base,
|
13
|
+
eligibility_error_message(:limit_once_per_user),
|
14
|
+
error_code: :limit_once_per_user
|
15
|
+
)
|
16
|
+
end
|
17
|
+
else
|
18
|
+
eligibility_errors.add(:base, eligibility_error_message(:no_user_specified), error_code: :no_user_specified)
|
19
|
+
end
|
20
|
+
|
21
|
+
eligibility_errors.empty?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class OptionValue < PromotionRule
|
6
|
+
include LineItemApplicableOrderRule
|
7
|
+
|
8
|
+
preference :eligible_values, :hash
|
9
|
+
|
10
|
+
def order_eligible?(order)
|
11
|
+
order.line_items.any? { |item| line_item_eligible?(item) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def line_item_eligible?(line_item)
|
15
|
+
LineItemOptionValue.new(preferred_eligible_values: preferred_eligible_values).eligible?(line_item)
|
16
|
+
end
|
17
|
+
|
18
|
+
def preferred_eligible_values
|
19
|
+
values = preferences[:eligible_values] || {}
|
20
|
+
values.keys.map(&:to_i).zip(
|
21
|
+
values.values.map do |value|
|
22
|
+
(value.is_a?(Array) ? value : value.split(",")).map(&:to_i)
|
23
|
+
end
|
24
|
+
).to_h
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
# A rule to limit a promotion based on products in the order. Can
|
6
|
+
# require all or any of the products to be present. Valid products
|
7
|
+
# either come from assigned product group or are assingned directly to
|
8
|
+
# the rule.
|
9
|
+
class Product < PromotionRule
|
10
|
+
include LineItemApplicableOrderRule
|
11
|
+
|
12
|
+
has_many :products_promotion_rules,
|
13
|
+
dependent: :destroy,
|
14
|
+
foreign_key: :promotion_rule_id,
|
15
|
+
class_name: "SolidusFriendlyPromotions::ProductsPromotionRule"
|
16
|
+
has_many :products, class_name: "Spree::Product", through: :products_promotion_rules
|
17
|
+
|
18
|
+
def preload_relations
|
19
|
+
[:products]
|
20
|
+
end
|
21
|
+
|
22
|
+
MATCH_POLICIES = %w[any all none only].freeze
|
23
|
+
|
24
|
+
validates :preferred_match_policy, inclusion: {in: MATCH_POLICIES}
|
25
|
+
|
26
|
+
preference :match_policy, :string, default: MATCH_POLICIES.first
|
27
|
+
|
28
|
+
# scope/association that is used to test eligibility
|
29
|
+
def eligible_products
|
30
|
+
products
|
31
|
+
end
|
32
|
+
|
33
|
+
def order_eligible?(order)
|
34
|
+
return true if eligible_products.empty?
|
35
|
+
|
36
|
+
case preferred_match_policy
|
37
|
+
when "all"
|
38
|
+
unless eligible_products.all? { |product| order_products(order).include?(product) }
|
39
|
+
eligibility_errors.add(:base, eligibility_error_message(:missing_product), error_code: :missing_product)
|
40
|
+
end
|
41
|
+
when "any"
|
42
|
+
unless order_products(order).any? { |product| eligible_products.include?(product) }
|
43
|
+
eligibility_errors.add(:base, eligibility_error_message(:no_applicable_products),
|
44
|
+
error_code: :no_applicable_products)
|
45
|
+
end
|
46
|
+
when "none"
|
47
|
+
unless order_products(order).none? { |product| eligible_products.include?(product) }
|
48
|
+
eligibility_errors.add(:base, eligibility_error_message(:has_excluded_product),
|
49
|
+
error_code: :has_excluded_product)
|
50
|
+
end
|
51
|
+
when "only"
|
52
|
+
unless order_products(order).all? { |product| eligible_products.include?(product) }
|
53
|
+
eligibility_errors.add(:base, eligibility_error_message(:has_excluded_product),
|
54
|
+
error_code: :has_excluded_product)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
raise "unexpected match policy: #{preferred_match_policy.inspect}"
|
58
|
+
end
|
59
|
+
|
60
|
+
eligibility_errors.empty?
|
61
|
+
end
|
62
|
+
|
63
|
+
def line_item_eligible?(line_item, _options = {})
|
64
|
+
# The order level eligibility check happens first, and if none of the products
|
65
|
+
# are in the order, then no line items should be available to check.
|
66
|
+
raise "This should not happen" if preferred_match_policy == "none"
|
67
|
+
product_ids.include?(line_item.variant.product_id)
|
68
|
+
end
|
69
|
+
|
70
|
+
def product_ids_string
|
71
|
+
product_ids.join(",")
|
72
|
+
end
|
73
|
+
|
74
|
+
def product_ids_string=(product_ids)
|
75
|
+
self.product_ids = product_ids.to_s.split(",").map(&:strip)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def order_products(order)
|
81
|
+
order.line_items.map(&:variant).map(&:product)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class ShippingMethod < PromotionRule
|
6
|
+
include ShipmentLevelRule
|
7
|
+
|
8
|
+
preference :shipping_method_ids, type: :array, default: []
|
9
|
+
|
10
|
+
def applicable?(promotable)
|
11
|
+
promotable.is_a?(Spree::Shipment) || promotable.is_a?(Spree::ShippingRate)
|
12
|
+
end
|
13
|
+
|
14
|
+
def eligible?(promotable)
|
15
|
+
promotable.shipping_method&.id&.in?(preferred_shipping_method_ids.map(&:to_i))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class Store < PromotionRule
|
6
|
+
include OrderLevelRule
|
7
|
+
|
8
|
+
has_many :promotion_rules_stores, class_name: "SolidusFriendlyPromotions::PromotionRulesStore",
|
9
|
+
foreign_key: :promotion_rule_id,
|
10
|
+
dependent: :destroy
|
11
|
+
has_many :stores, through: :promotion_rules_stores, class_name: "Spree::Store"
|
12
|
+
|
13
|
+
def preload_relations
|
14
|
+
[:stores]
|
15
|
+
end
|
16
|
+
|
17
|
+
def eligible?(order, _options = {})
|
18
|
+
stores.none? || stores.include?(order.store)
|
19
|
+
end
|
20
|
+
|
21
|
+
def updateable?
|
22
|
+
true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class Taxon < PromotionRule
|
6
|
+
include LineItemApplicableOrderRule
|
7
|
+
|
8
|
+
has_many :promotion_rules_taxons, class_name: "SolidusFriendlyPromotions::PromotionRulesTaxon", foreign_key: :promotion_rule_id,
|
9
|
+
dependent: :destroy
|
10
|
+
has_many :taxons, through: :promotion_rules_taxons, class_name: "Spree::Taxon"
|
11
|
+
|
12
|
+
def preload_relations
|
13
|
+
[:taxons]
|
14
|
+
end
|
15
|
+
|
16
|
+
MATCH_POLICIES = %w[any all none].freeze
|
17
|
+
|
18
|
+
validates :preferred_match_policy, inclusion: {in: MATCH_POLICIES}
|
19
|
+
|
20
|
+
preference :match_policy, :string, default: MATCH_POLICIES.first
|
21
|
+
|
22
|
+
def order_eligible?(order)
|
23
|
+
order_taxons = taxons_in_order(order)
|
24
|
+
|
25
|
+
case preferred_match_policy
|
26
|
+
when "all"
|
27
|
+
matches_all = taxons.all? do |rule_taxon|
|
28
|
+
order_taxons.where(id: rule_taxon.self_and_descendants.ids).exists?
|
29
|
+
end
|
30
|
+
|
31
|
+
unless matches_all
|
32
|
+
eligibility_errors.add(:base, eligibility_error_message(:missing_taxon), error_code: :missing_taxon)
|
33
|
+
end
|
34
|
+
when "any"
|
35
|
+
unless order_taxons.where(id: rule_taxon_ids_with_children).exists?
|
36
|
+
eligibility_errors.add(
|
37
|
+
:base,
|
38
|
+
eligibility_error_message(:no_matching_taxons),
|
39
|
+
error_code: :no_matching_taxons
|
40
|
+
)
|
41
|
+
end
|
42
|
+
when "none"
|
43
|
+
if order_taxons.where(id: rule_taxon_ids_with_children).exists?
|
44
|
+
eligibility_errors.add(
|
45
|
+
:base,
|
46
|
+
eligibility_error_message(:has_excluded_taxon),
|
47
|
+
error_code: :has_excluded_taxon
|
48
|
+
)
|
49
|
+
end
|
50
|
+
else
|
51
|
+
raise "unexpected match policy: #{preferred_match_policy.inspect}"
|
52
|
+
end
|
53
|
+
|
54
|
+
eligibility_errors.empty?
|
55
|
+
end
|
56
|
+
|
57
|
+
def line_item_eligible?(line_item)
|
58
|
+
# The order level eligibility check happens first, and if none of the taxons
|
59
|
+
# are in the order, then no line items should be available to check.
|
60
|
+
raise "This should not happen" if preferred_match_policy == "none"
|
61
|
+
|
62
|
+
raise "unexpected match policy: #{preferred_match_policy.inspect}" unless preferred_match_policy.in?(MATCH_POLICIES)
|
63
|
+
|
64
|
+
Spree::Classification.where(
|
65
|
+
product_id: line_item.variant.product_id,
|
66
|
+
taxon_id: rule_taxon_ids_with_children
|
67
|
+
).exists?
|
68
|
+
end
|
69
|
+
|
70
|
+
def taxon_ids_string
|
71
|
+
taxon_ids.join(",")
|
72
|
+
end
|
73
|
+
|
74
|
+
def taxon_ids_string=(taxon_ids)
|
75
|
+
self.taxon_ids = taxon_ids.to_s.split(",").map(&:strip)
|
76
|
+
end
|
77
|
+
|
78
|
+
def updateable?
|
79
|
+
true
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
# All taxons in an order
|
85
|
+
def taxons_in_order(order)
|
86
|
+
Spree::Taxon
|
87
|
+
.joins(products: {variants_including_master: :line_items})
|
88
|
+
.where(spree_line_items: {order_id: order.id})
|
89
|
+
.distinct
|
90
|
+
end
|
91
|
+
|
92
|
+
# ids of taxons rules and taxons rules children
|
93
|
+
def rule_taxon_ids_with_children
|
94
|
+
taxons.flat_map { |taxon| taxon.self_and_descendants.ids }.uniq
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class User < PromotionRule
|
6
|
+
include OrderLevelRule
|
7
|
+
|
8
|
+
has_many :promotion_rules_users,
|
9
|
+
class_name: "SolidusFriendlyPromotions::PromotionRulesUser",
|
10
|
+
foreign_key: :promotion_rule_id,
|
11
|
+
dependent: :destroy
|
12
|
+
has_many :users, through: :promotion_rules_users, class_name: Spree::UserClassHandle.new
|
13
|
+
|
14
|
+
def preload_relations
|
15
|
+
[:users]
|
16
|
+
end
|
17
|
+
|
18
|
+
def eligible?(order, _options = {})
|
19
|
+
users.include?(order.user)
|
20
|
+
end
|
21
|
+
|
22
|
+
def user_ids_string
|
23
|
+
user_ids.join(",")
|
24
|
+
end
|
25
|
+
|
26
|
+
def user_ids_string=(user_ids)
|
27
|
+
self.user_ids = user_ids.to_s.split(",").map(&:strip)
|
28
|
+
end
|
29
|
+
|
30
|
+
def updateable?
|
31
|
+
true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class UserLoggedIn < PromotionRule
|
6
|
+
include OrderLevelRule
|
7
|
+
|
8
|
+
def eligible?(order, _options = {})
|
9
|
+
if order.user.blank?
|
10
|
+
eligibility_errors.add(:base, eligibility_error_message(:no_user_specified), error_code: :no_user_specified)
|
11
|
+
end
|
12
|
+
eligibility_errors.empty?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
module Rules
|
5
|
+
class UserRole < PromotionRule
|
6
|
+
include OrderLevelRule
|
7
|
+
|
8
|
+
preference :role_ids, :array, default: []
|
9
|
+
|
10
|
+
MATCH_POLICIES = %w[any all].freeze
|
11
|
+
preference :match_policy, default: MATCH_POLICIES.first
|
12
|
+
|
13
|
+
def eligible?(order, _options = {})
|
14
|
+
return false unless order.user
|
15
|
+
|
16
|
+
if all_match_policy?
|
17
|
+
match_all_roles?(order)
|
18
|
+
else
|
19
|
+
match_any_roles?(order)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def all_match_policy?
|
26
|
+
preferred_match_policy == "all" && preferred_role_ids.present?
|
27
|
+
end
|
28
|
+
|
29
|
+
def user_roles(order)
|
30
|
+
order.user.spree_roles.where(id: preferred_role_ids)
|
31
|
+
end
|
32
|
+
|
33
|
+
def match_all_roles?(order)
|
34
|
+
user_roles(order).count == preferred_role_ids.count
|
35
|
+
end
|
36
|
+
|
37
|
+
def match_any_roles?(order)
|
38
|
+
user_roles(order).exists?
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class ShippingRateDiscount < Spree::Base
|
5
|
+
belongs_to :shipping_rate, inverse_of: :discounts, class_name: "Spree::ShippingRate"
|
6
|
+
belongs_to :promotion_action, -> { with_discarded }, inverse_of: false
|
7
|
+
|
8
|
+
extend Spree::DisplayMoney
|
9
|
+
money_methods :amount
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SolidusFriendlyPromotions
|
4
|
+
class SimpleOrderContents < Spree::OrderContents
|
5
|
+
def update_cart(params)
|
6
|
+
if order.update(params)
|
7
|
+
unless order.completed?
|
8
|
+
order.line_items = order.line_items.select { |li| li.quantity > 0 }
|
9
|
+
order.check_shipments_and_restart_checkout
|
10
|
+
end
|
11
|
+
reload_totals
|
12
|
+
true
|
13
|
+
else
|
14
|
+
false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def after_add_or_remove(line_item, options = {})
|
21
|
+
shipment = options[:shipment]
|
22
|
+
shipment.present? ? shipment.update_amounts : order.check_shipments_and_restart_checkout
|
23
|
+
reload_totals
|
24
|
+
line_item
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/app/views/solidus_friendly_promotions/admin/promotion_actions/_calculator_select.html.erb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
<%= form_with model: promotion_action, scope: :promotion_action, url: path, method: :get do |form| %>
|
2
|
+
<div class="field">
|
3
|
+
<%= form.hidden_field :type %>
|
4
|
+
<%= form.label :calculator_type %>
|
5
|
+
<%=
|
6
|
+
form.select :calculator_type,
|
7
|
+
options_for_promotion_action_calculator_types(form.object),
|
8
|
+
{
|
9
|
+
include_blank: t(:choose_promotion_action_calculator, scope: 'solidus_friendly_promotions')
|
10
|
+
},
|
11
|
+
class: 'custom-select fullwidth',
|
12
|
+
onchange: 'this.form.requestSubmit()',
|
13
|
+
required: true
|
14
|
+
%>
|
15
|
+
</div>
|
16
|
+
<% end %>
|
@@ -0,0 +1,29 @@
|
|
1
|
+
<%= turbo_frame_tag @promotion, dom_id(promotion_action) do %>
|
2
|
+
<div class="promotion_action promotion-block">
|
3
|
+
<h6 class='promotion-title'> <%= promotion_action.model_name.human %></h6>
|
4
|
+
|
5
|
+
<% if can?(:destroy, promotion_action) %>
|
6
|
+
<%= link_to_with_icon 'trash', '', solidus_friendly_promotions.admin_promotion_promotion_action_path(@promotion, promotion_action), method: :delete, class: 'delete' %>
|
7
|
+
<% end %>
|
8
|
+
|
9
|
+
|
10
|
+
<%= render "solidus_friendly_promotions/admin/promotion_actions/calculator_select",
|
11
|
+
path: solidus_friendly_promotions.edit_admin_promotion_promotion_action_path(@promotion, promotion_action),
|
12
|
+
promotion_action: promotion_action %>
|
13
|
+
|
14
|
+
<%=
|
15
|
+
form_with(
|
16
|
+
model: promotion_action,
|
17
|
+
scope: :promotion_action,
|
18
|
+
url: solidus_friendly_promotions.admin_promotion_promotion_action_path(@promotion, promotion_action),
|
19
|
+
data: { turbo: false }
|
20
|
+
) do |form| %>
|
21
|
+
<%= render 'solidus_friendly_promotions/admin/promotion_actions/form', form: form %>
|
22
|
+
<div class="row">
|
23
|
+
<div class="col-12">
|
24
|
+
<%= button_tag "Update", class: "btn btn-secondary float-right" %>
|
25
|
+
</div>
|
26
|
+
</div>
|
27
|
+
<% end %>
|
28
|
+
</div>
|
29
|
+
<% end %>
|