Skip to content

Instantly share code, notes, and snippets.

@cesartalves
Last active July 1, 2021 08:37
Show Gist options
  • Save cesartalves/cae172c6bdad6570b279ca3c39862a5e to your computer and use it in GitHub Desktop.
Save cesartalves/cae172c6bdad6570b279ca3c39862a5e to your computer and use it in GitHub Desktop.
Update Tax Total / Shipping Total
# note: this assumes the order never gets recalculated
csv = CSV.read('maker-orders.csv', headers: true)
orders = []
csv.each do |context|
order = Spree::Order.find_by_number(context['Name'])
next if order.nil?
tax_total = context['Tax Total'].to_f
ship_total = context['Shipping Total'].to_f
# use item_total instead of total because that makes updates idempotent. We don't need to worry about
# orders with multiple csv lines
order.update_columns(
total: order.item_total + tax_total + ship_total,
additional_tax_total: tax_total,
shipment_total: ship_total,
payment_total: order.item_total + tax_total + ship_total
)
orders << order
end
affected = orders.uniq
puts "Affected:", affected.size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment