Skip to content

Instantly share code, notes, and snippets.

@onedanshow
Created November 26, 2014 02:22
Show Gist options
  • Save onedanshow/bbcd328d3c6aa8320338 to your computer and use it in GitHub Desktop.
Save onedanshow/bbcd328d3c6aa8320338 to your computer and use it in GitHub Desktop.
Way to pick the best promotion between order adjustments and line item adjustments in Spree 2.2+
Spree::OrderUpdater.class_eval do
module BetterPromotions
# DD: called by 'update_adjustment_total' before setting totals on order
def recalculate_adjustments
# DD: first, calculate all_adjustments for Order, LineItem, and Shipment using Spree::ItemAdjustments
super
# DD: FYI: promo_totals are negative
if order.line_items.sum(:promo_total) < order.adjustments.promotion.eligible.sum(:amount)
# DD: if line items offer a better promo, make order adjustments ineligible
order.adjustments.promotion.eligible.update_all(eligible:false)
else
# DD: if order has a better promo, make line item adjustments ineligible
# and undo totals set by Spree::ItemAdjustments#update_adjustments
order.all_adjustments.where(adjustable_type: 'Spree::LineItem').promotion.eligible.update_all(eligible:false)
order.line_items.update_all("adjustment_total = additional_tax_total, promo_total = 0")
end
end
end
prepend BetterPromotions
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment