Sha256: e2868c0e9e9251f93c110394b5a7e9a6a7eec6bc65a41efb91273e218a712f34

Contents?: true

Size: 1.33 KB

Versions: 36

Compression:

Stored size: 1.33 KB

Contents

module Spree
  class Variant
    # This class is responsible for selecting a price for a variant given certain pricing options.
    # A variant can have multiple or even dynamic prices. The `price_for`
    # method determines which price applies under the given circumstances.
    #
    class PriceSelector
      # The pricing options represent "given circumstances" for a price: The currency
      # we need and the country that the price applies to.
      # Every price selector is designed to work with a particular set of pricing options
      # embodied in it's pricing options class.
      #
      def self.pricing_options_class
        Spree::Variant::PricingOptions
      end

      attr_reader :variant

      def initialize(variant)
        @variant = variant
      end

      # The variant's price, given a set of pricing options
      # @param [Spree::Variant::PricingOptions] price_options Pricing Options to abide by
      # @return [Spree::Money, nil] The most specific price for this set of pricing options.
      def price_for(price_options)
        variant.currently_valid_prices.detect do |price|
          ( price.country_iso == price_options.desired_attributes[:country_iso] ||
            price.country_iso.nil?
          ) && price.currency == price_options.desired_attributes[:currency]
        end.try!(:money)
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
solidus_core-2.4.2 app/models/spree/variant/price_selector.rb
solidus_core-2.3.1 app/models/spree/variant/price_selector.rb
solidus_core-2.4.1 app/models/spree/variant/price_selector.rb
solidus_core-2.2.2 app/models/spree/variant/price_selector.rb
solidus_core-2.1.1 app/models/spree/variant/price_selector.rb
solidus_core-2.0.3 app/models/spree/variant/price_selector.rb
solidus_core-1.4.2 app/models/spree/variant/price_selector.rb
solidus_core-1.3.2 app/models/spree/variant/price_selector.rb
solidus_core-2.4.0 app/models/spree/variant/price_selector.rb
solidus_core-2.4.0.rc1 app/models/spree/variant/price_selector.rb
solidus_core-2.4.0.beta1 app/models/spree/variant/price_selector.rb
solidus_core-2.3.0 app/models/spree/variant/price_selector.rb
solidus_core-2.3.0.rc3 app/models/spree/variant/price_selector.rb
solidus_core-2.3.0.rc2 app/models/spree/variant/price_selector.rb
solidus_core-2.3.0.rc1 app/models/spree/variant/price_selector.rb
solidus_core-2.3.0.beta1 app/models/spree/variant/price_selector.rb
solidus_core-2.0.2 app/models/spree/variant/price_selector.rb
solidus_core-1.4.1 app/models/spree/variant/price_selector.rb
solidus_core-2.2.1 app/models/spree/variant/price_selector.rb
solidus_core-2.2.0 app/models/spree/variant/price_selector.rb