Class: Spree::Orders::UpsertItems

Inherits:
Object
  • Object
show all
Includes:
ServiceModule::Base
Defined in:
app/services/spree/orders/upsert_items.rb

Overview

Bulk upsert line items on an order. Mirrors Spree::Carts::UpsertItems but is admin/order-side (separate from the cart pipeline per the 6.0 cart/order split — see docs/plans/6.0-cart-order-split.md).

For each entry in items:

  • If a line item for the variant already exists -> sets its quantity

  • If no line item exists -> creates one with the given quantity

Order totals are NOT recalculated here. Callers (Spree::Orders::Create and Spree::Orders::Update) are responsible for running shipment rebuilding and a final ‘order.update_with_updater!` once their full pipeline (items, shipments, coupons) has run.

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(order:, items:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/spree/orders/upsert_items.rb', line 18

def call(order:, items:)
  items = Array(items)
  return success(order) if items.empty?

  store = order.store || Spree::Current.store

  ApplicationRecord.transaction do
    items.each do |item_params|
      item_params = item_params.to_h.deep_symbolize_keys
      variant = resolve_variant(store, item_params[:variant_id])
      next unless variant

      quantity = (item_params[:quantity] || 1).to_i
      next if quantity <= 0

      return failure(variant, "#{variant.name} is not available in #{order.currency}") if variant.amount_in(order.currency).nil?

      line_item = Spree.line_item_by_variant_finder.new.execute(order: order, variant: variant)

      if line_item
        line_item.quantity = quantity
        line_item. = line_item..merge(item_params[:metadata].to_h) if item_params[:metadata].present?
      else
        line_item = order.line_items.new(quantity: quantity, variant: variant, options: { currency: order.currency })
        line_item. = item_params[:metadata].to_h if item_params[:metadata].present?
      end

      return failure(line_item) unless line_item.save
    end
  end

  success(order)
end