Sha256: d395c958e9d44036ae679a48609546d1fad835933b8662a08a700db21ef9d010

Contents?: true

Size: 1.27 KB

Versions: 19

Compression:

Stored size: 1.27 KB

Contents

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

      def call(order:, params:)
        return failure(order) unless order.update_attributes(filter_order_items(order, params))

        order.line_items = order.line_items.select { |li| li.quantity > 0 }
        # Update totals, then check if the order is eligible for any cart promotions.
        # If we do not update first, then the item total will be wrong and ItemTotal
        # promotion rules would not be triggered.
        ActiveRecord::Base.transaction do
          order.update_with_updater!
          ::Spree::PromotionHandler::Cart.new(order).activate
          order.ensure_updated_shipments
          order.payments.store_credits.checkout.destroy_all
          order.update_with_updater!
        end
        success(order)
      end

      private

      def filter_order_items(order, params)
        return params if params[:line_items_attributes].nil? || params[:line_items_attributes][:id]

        line_item_ids = order.line_items.pluck(:id)

        params[:line_items_attributes].each_pair do |id, value|
          params[:line_items_attributes].delete(id) unless line_item_ids.include?(value[:id].to_i) || value[:variant_id].present?
        end
        params
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
spree_core-3.7.14.1 app/services/spree/cart/update.rb
spree_core-3.7.14 app/services/spree/cart/update.rb
spree_core-3.7.13 app/services/spree/cart/update.rb
spree_core-3.7.12 app/services/spree/cart/update.rb
spree_core-3.7.11 app/services/spree/cart/update.rb
spree_core-3.7.10 app/services/spree/cart/update.rb
spree_core-3.7.9 app/services/spree/cart/update.rb
spree_core-3.7.8 app/services/spree/cart/update.rb
spree_core-3.7.7 app/services/spree/cart/update.rb
spree_core-3.7.6 app/services/spree/cart/update.rb
spree_core-3.7.5 app/services/spree/cart/update.rb
spree_core-3.7.4 app/services/spree/cart/update.rb
spree_core-3.7.3 app/services/spree/cart/update.rb
spree_core-3.7.2 app/services/spree/cart/update.rb
spree_core-3.7.1 app/services/spree/cart/update.rb
spree_core-3.7.0 app/services/spree/cart/update.rb
spree_core-3.7.0.rc3 app/services/spree/cart/update.rb
spree_core-3.7.0.rc2 app/services/spree/cart/update.rb
spree_core-3.7.0.rc1 app/services/spree/cart/update.rb