Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add revert and deactivate methods to allow actions to take place when… #7705

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions core/app/models/spree/promotion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,33 @@ def activate(payload)
action_taken
end

# Called when a promotion is removed from the cart
def deactivate(payload)
order = payload[:order]
return unless self.class.order_activatable?(order)

payload[:promotion] = self

# Track results from actions to see if any action has been taken.
# Actions should return nil/false if no action has been taken.
# If an action returns true, then an action has been taken.
results = actions.map do |action|
action.revert(payload) if action.respond_to?(:revert)
end

# If an action has been taken, report back to whatever `d this promotion.
action_taken = results.include?(true)

if action_taken
# connect to the order
# create the join_table entry.
orders << order
save
end

action_taken
end

# called anytime order.update_with_updater! happens
def eligible?(promotable)
return false if expired? || usage_limit_exceeded?(promotable) || blacklisted?(promotable)
Expand Down
22 changes: 20 additions & 2 deletions core/app/models/spree/promotion/actions/create_line_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CreateLineItems < PromotionAction
# needs to be manually removed from the order by the customer
def perform(options = {})
order = options[:order]
return unless self.eligible? order
return unless eligible? order

action_taken = false
promotion_action_line_items.each do |item|
Expand All @@ -50,12 +50,30 @@ def perform(options = {})
action_taken
end

# Called by promotion handler when a promotion is removed
# This will find any line item matching the ones defined in the PromotionAction
# and remove the same quantity as was added by the PromotionAction.
# Should help to prevent some of cases listed above the #perform method
def revert(options = {})
order = options[:order]
return if eligible?(order)

action_taken = false
promotion_action_line_items.each do |item|
line_item = order.find_line_item_by_variant(item.variant)
next unless line_item.present?
order.contents.remove(item.variant, (item.quantity || 1))
action_taken = true
end

action_taken
end

# Checks that there's enough stock to add the line item to the order
def item_available?(item)
quantifier = Spree::Stock::Quantifier.new(item.variant)
quantifier.can_supply? item.quantity
end

end
end
end
Expand Down
2 changes: 2 additions & 0 deletions core/app/models/spree/promotion_handler/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def activate
promotions.each do |promotion|
if (line_item && promotion.eligible?(line_item)) || promotion.eligible?(order)
promotion.activate(line_item: line_item, order: order)
else
promotion.deactivate(line_item: line_item, order: order)
end
end
end
Expand Down