1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-15 00:24:09 +01:00
fab-manager/app/services/cart/update_total_service.rb
2023-02-15 10:29:53 +01:00

26 lines
559 B
Ruby

# frozen_string_literal: true
# Provides methods for update total of cart
class Cart::UpdateTotalService
# @param order[Order]
def call(order)
total = 0
order.order_items.each do |item|
update_item_price(item)
total += (item.amount * item.quantity) unless item.is_offered
end
order.total = total
order.save
order
end
private
# @param item[OrderItem]
def update_item_price(item)
return unless item.orderable_type.match(/^CartItem::/)
item.update(amount: item.orderable.price[:amount] || 0)
end
end