diff --git a/promotions/app/models/solidus_friendly_promotions/actions/create_discounted_item.rb b/promotions/app/models/solidus_friendly_promotions/actions/create_discounted_item.rb index d17e6fb9..58096dff 100644 --- a/promotions/app/models/solidus_friendly_promotions/actions/create_discounted_item.rb +++ b/promotions/app/models/solidus_friendly_promotions/actions/create_discounted_item.rb @@ -6,6 +6,7 @@ class CreateDiscountedItem < PromotionAction include OrderLevelAction preference :variant_id, :integer preference :quantity, :integer, default: 1 + preference :necessary_quantity, :integer, default: 1 def perform(order) line_item = find_item(order) || create_item(order) @@ -30,7 +31,10 @@ def create_item(order) def determine_item_quantity(order) applicable_line_items = promotion.applicable_line_items(order) - applicable_line_items.sum(&:quantity) * preferred_quantity + # Integer division will floor automatically, which is what we want here: + # 1 Item, 2 needed: 1 * 1 / 2 => 0 + # 5 items, 2 preferred, 2 needed: 5 / 2 * 2 => 4 + applicable_line_items.sum(&:quantity) / preferred_necessary_quantity * preferred_quantity end def set_quantity(line_item, quantity) diff --git a/promotions/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_create_discounted_item.html.erb b/promotions/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_create_discounted_item.html.erb index 87da02ec..ae6abb19 100644 --- a/promotions/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_create_discounted_item.html.erb +++ b/promotions/app/views/solidus_friendly_promotions/admin/promotion_actions/actions/_create_discounted_item.html.erb @@ -5,6 +5,10 @@ <%= form.label :preferred_quantity %> <%= form.number_field :preferred_quantity, class: "fullwidth" %> +