Skip to content

Commit

Permalink
Make CreateDiscountedItem able to do Buy N get N free
Browse files Browse the repository at this point in the history
There's two quantities we want to configure. Buy X get Y free...
  • Loading branch information
mamhoff committed Nov 10, 2023
1 parent 322dae1 commit 7a1aaa6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<%= form.label :preferred_quantity %>
<%= form.number_field :preferred_quantity, class: "fullwidth" %>
</div>
<div class="field">
<%= form.label :preferred_necessary_quantity %>
<%= form.number_field :preferred_necessary_quantity, class: "fullwidth" %>
</div>
<% end %>

<%= render(
Expand Down
4 changes: 3 additions & 1 deletion promotions/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ en:
solidus_friendly_promotions/actions/adjust_shipment:
description: Creates a promotion credit on matching shipments
solidus_friendly_promotions/actions/create_discounted_item:
description: Creates a discounted item
description: Creates a discounted item with the quantity the applicable line items.
preferred_quantity: Quantity per applicable line item quantity
preferred_necessary_quantity: Number of items needed for a discounted item
solidus_friendly_promotions/rules/first_order:
description: Must be the customer's first order
solidus_friendly_promotions/rules/first_repeat_purchase_since:
Expand Down

0 comments on commit 7a1aaa6

Please sign in to comment.