Skip to content

Instantly share code, notes, and snippets.

@HoyaBoya
Created March 5, 2014 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HoyaBoya/9377392 to your computer and use it in GitHub Desktop.
Save HoyaBoya/9377392 to your computer and use it in GitHub Desktop.
##
# This is spree 2-1-stable specific logic to re-apply a promotion code. It will not work for 2-2-stable because promotions are being revamped.
# https://github.com/spree/spree/issues/4398
#
def reapply_coupon_codes
codes = []
# Go through the adjustments to find promotions that are not eligible
adjustments.each do |a|
if a.eligible == false && a.originator.respond_to?(:promotion) && a.originator.promotion.code
codes << a.originator.promotion.code
end
end
# Re-apply each code...depend on Spree to use the best one.
# https://github.com/spree/spree/blob/2-1-stable/api/app/controllers/spree/api/orders_controller.rb#L85
codes.each do |c|
coupon_code = c
save
Spree::Promo::CouponApplicator.new(self).apply
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment