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

Version Path
spree_core-4.0.9 app/services/spree/cart/add_item.rb
spree_core-4.1.15 app/services/spree/cart/add_item.rb
spree_core-4.2.7 app/services/spree/cart/add_item.rb
spree_core-4.3.3 app/services/spree/cart/add_item.rb
spree_core-4.2.6 app/services/spree/cart/add_item.rb
spree_core-4.3.2 app/services/spree/cart/add_item.rb
spree_core-4.3.1 app/services/spree/cart/add_item.rb
spree_core-4.3.0 app/services/spree/cart/add_item.rb
spree_core-4.3.0.rc3 app/services/spree/cart/add_item.rb
spree_core-4.3.0.rc2 app/services/spree/cart/add_item.rb
spree_core-4.3.0.rc1 app/services/spree/cart/add_item.rb
spree_core-4.2.5 app/services/spree/cart/add_item.rb
spree_core-4.1.14 app/services/spree/cart/add_item.rb
spree_core-4.0.8 app/services/spree/cart/add_item.rb
spree_core-4.2.4 app/services/spree/cart/add_item.rb
spree_core-4.2.3.1 app/services/spree/cart/add_item.rb
spree_core-4.1.13.1 app/services/spree/cart/add_item.rb
spree_core-4.0.7.1 app/services/spree/cart/add_item.rb
spree_core-4.2.3 app/services/spree/cart/add_item.rb
spree_core-4.2.2 app/services/spree/cart/add_item.rb