Sha256: 6588e9fe6414f36ac08b6cde35d123905bba0ff59bc39e5a2f4efed8fc96bc4e

Contents?: true

Size: 1.21 KB

Versions: 100

Compression:

Stored size: 1.21 KB

Contents

module Spree
  module Cart
    class RemoveItem
      prepend Spree::ServiceModule::Base

      def call(order:, variant:, quantity: nil, options: nil)
        options ||= {}
        quantity ||= 1

        ActiveRecord::Base.transaction do
          line_item = remove_from_line_item(order: order, variant: variant, quantity: quantity, options: options)
          Spree::Dependencies.cart_recalculate_service.constantize.call(line_item: line_item,
                                                                        order: order,
                                                                        options: options)
          success(line_item)
        end
      end

      private

      def remove_from_line_item(order:, variant:, quantity:, options:)
        line_item = Spree::Dependencies.line_item_by_variant_finder.constantize.new.execute(order: order, variant: variant, options: options)

        raise ActiveRecord::RecordNotFound if line_item.nil?

        line_item.quantity -= quantity
        line_item.target_shipment = options[:shipment]

        if line_item.quantity.zero?
          order.line_items.destroy(line_item)
        else
          line_item.save!
        end

        line_item
      end
    end
  end
end

Version data entries

100 entries across 100 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/services/spree/cart/remove_item.rb
spree_core-4.10.0 app/services/spree/cart/remove_item.rb
spree_core-4.9.0 app/services/spree/cart/remove_item.rb
spree_core-4.8.3 app/services/spree/cart/remove_item.rb
spree_core-4.8.2 app/services/spree/cart/remove_item.rb
spree_core-4.7.3 app/services/spree/cart/remove_item.rb
spree_core-4.6.6 app/services/spree/cart/remove_item.rb
spree_core-4.5.5 app/services/spree/cart/remove_item.rb
spree_core-4.0.9 app/services/spree/cart/remove_item.rb
spree_core-4.1.15 app/services/spree/cart/remove_item.rb
spree_core-4.2.7 app/services/spree/cart/remove_item.rb
spree_core-4.3.3 app/services/spree/cart/remove_item.rb
spree_core-4.4.1 app/services/spree/cart/remove_item.rb
spree_core-4.5.4 app/services/spree/cart/remove_item.rb
spree_core-4.6.5 app/services/spree/cart/remove_item.rb
spree_core-4.7.2 app/services/spree/cart/remove_item.rb
spree_core-4.7.1 app/services/spree/cart/remove_item.rb
spree_core-4.6.4 app/services/spree/cart/remove_item.rb
spree_core-4.7.0 app/services/spree/cart/remove_item.rb
spree_core-4.6.3 app/services/spree/cart/remove_item.rb