11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'app/models/spree/store_product.rb', line 11
def refresh_metrics!
return if product.nil?
completed_order_ids = product.completed_orders.where(store_id: store_id).select(:id)
variant_ids = product.variants_including_master.ids
line_items = Spree::LineItem.joins(:order)
.where(spree_orders: { id: completed_order_ids })
.where(variant_id: variant_ids)
update!(
units_sold_count: line_items.sum(:quantity),
revenue: line_items.sum(:pre_tax_amount)
)
end
|