Sha256: 3961558b68a23907dbc2d0d4e7ad306a2a5e93f6f9f551f21869a07a38cdc654

Contents?: true

Size: 1.06 KB

Versions: 30

Compression:

Stored size: 1.06 KB

Contents

module Spree
  class Order < Spree::Base
    module CurrencyUpdater
      extend ActiveSupport::Concern # FIXME: this module is not required to be a concern

      included do
        def homogenize_line_item_currencies
          update_line_item_currencies!
          update_with_updater!
        end
      end

      # Updates prices of order's line items
      def update_line_item_currencies!
        line_items.where.not(currency: currency).each do |line_item|
          update_line_item_price!(line_item)
        end
      end

      # Returns the price object from given item
      def price_from_line_item(line_item)
        line_item.variant.prices.where(currency: currency).first
      end

      # Updates price from given line item
      def update_line_item_price!(line_item)
        price = price_from_line_item(line_item)

        if price
          line_item.update!(currency: price.currency, price: price.amount)
        else
          raise "no #{currency} price found for #{line_item.product.name} (#{line_item.variant.sku})"
        end
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
spree_core-4.8.3 app/models/spree/order/currency_updater.rb
spree_core-4.8.2 app/models/spree/order/currency_updater.rb
spree_core-4.7.3 app/models/spree/order/currency_updater.rb
spree_core-4.6.6 app/models/spree/order/currency_updater.rb
spree_core-4.5.5 app/models/spree/order/currency_updater.rb
spree_core-4.3.3 app/models/spree/order/currency_updater.rb
spree_core-4.4.1 app/models/spree/order/currency_updater.rb
spree_core-4.5.4 app/models/spree/order/currency_updater.rb
spree_core-4.6.5 app/models/spree/order/currency_updater.rb
spree_core-4.7.2 app/models/spree/order/currency_updater.rb
spree_core-4.7.1 app/models/spree/order/currency_updater.rb
spree_core-4.6.4 app/models/spree/order/currency_updater.rb
spree_core-4.7.0 app/models/spree/order/currency_updater.rb
spree_core-4.6.3 app/models/spree/order/currency_updater.rb
spree_core-4.6.2 app/models/spree/order/currency_updater.rb
spree_core-4.6.1 app/models/spree/order/currency_updater.rb
spree_core-4.6.0 app/models/spree/order/currency_updater.rb
spree_core-4.5.3 app/models/spree/order/currency_updater.rb
spree_core-4.5.2 app/models/spree/order/currency_updater.rb
spree_core-4.5.1 app/models/spree/order/currency_updater.rb