Sha256: 9bee8786d4e2fa0ce034bae5827d4aa449a6f8149d4860a5f146f4441f2d0450
Contents?: true
Size: 1.52 KB
Versions: 57
Compression:
Stored size: 1.52 KB
Contents
module Spree module Cart class AddItem prepend Spree::ServiceModule::Base def call(order:, variant:, quantity: nil, options: {}) ApplicationRecord.transaction do run :add_to_line_item run Spree::Dependencies.cart_recalculate_service.constantize end end private def add_to_line_item(order:, variant:, quantity: nil, options: {}) options ||= {} quantity ||= 1 line_item = Spree::Dependencies.line_item_by_variant_finder.constantize.new.execute(order: order, variant: variant, options: options) line_item_created = line_item.nil? if line_item.nil? opts = ::Spree::PermittedAttributes.line_item_attributes.flatten.each_with_object({}) do |attribute, result| result[attribute] = options[attribute] end.merge(currency: order.currency).delete_if { |_key, value| value.nil? } line_item = order.line_items.new(quantity: quantity, variant: variant, options: opts) else line_item.quantity += quantity.to_i end line_item.target_shipment = options[:shipment] if options.key? :shipment return failure(line_item) unless line_item.save line_item.reload.update_price ::Spree::TaxRate.adjust(order, [line_item]) if line_item_created success(order: order, line_item: line_item, line_item_created: line_item_created, options: options) end end end end
Version data entries
57 entries across 57 versions & 1 rubygems