Class: Spree::Variants::RemoveLineItemJob
- Defined in:
- app/jobs/spree/variants/remove_line_item_job.rb
Defined Under Namespace
Classes: RemovalRejected
Constant Summary
Constants inherited from BaseJob
Instance Method Summary collapse
Instance Method Details
#perform(line_item:) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/jobs/spree/variants/remove_line_item_job.rb', line 6 def perform(line_item:) owner = line_item.owner return if owner.nil? # A completed cart is a read-only audit record; the order it became # owns the lines now. The cart can complete between this job being # enqueued and running, and there is nothing left to remove from it. return if owner.is_a?(Spree::Cart) && owner.readonly? # Carts and draft orders have different contracts: the cart side turns # a vetoed removal into a warning and still succeeds, the order side # fails outright. Routing an order-owned line through the cart # workflow would apply cart semantics to an admin edit. cart_owned = owner.is_a?(Spree::Cart) workflow = (cart_owned ? Spree.cart_upsert_items_workflow : Spree.order_upsert_items_workflow).new result = workflow.call( **(cart_owned ? { cart: owner } : { order: owner }), items: [{ variant_id: line_item.variant_id, quantity: 0 }] ) # A cart-side veto surfaces as a warning rather than a failed result, # so both have to be checked — otherwise a discontinued variant stays # in the cart with nothing to show for it. rejection = workflow.warnings.first return if result.success? && rejection.nil? Rails.error.report( RemovalRejected.new(rejection&. || result.error.to_s), context: { line_item_id: line_item.id, variant_id: line_item.variant_id }, source: 'spree.variants.remove_line_item', handled: true ) end |