Sha256: 1cab4cd1b02a220b804ffaa692d59c46c52448276a71591d89664c909d618a51

Contents?: true

Size: 1.89 KB

Versions: 69

Compression:

Stored size: 1.89 KB

Contents

module Spree::ZonePricing::Variant

  def self.included(target)
    target.class_eval do
      has_many :zone_prices, :dependent => :destroy
      has_many :zones, :through => :zone_prices

      # Update zone prices for variant from attributes
      # passed to us from the form
      def zone_price_attributes=(price_attributes)
        price_attributes.each do |key, value|
          # Check if record exists, if so update. If value of price
          # is blank or 0 then delete record
          zone_price = ZonePrice.first(:conditions => {:zone_id => key, :variant_id => self.id})
          if zone_price
            # Record exists, update or delete if no price specified
            if value['price'].to_f != 0
              zone_price.update_attribute(:price, value['price'].to_f)
            else
              zone_price.destroy
            end
          elsif value['price'].to_f != 0
            # Record does not exist, create
            ZonePrice.create(:zone_id => key, :variant => self, :price => value['price'].to_f)
          end
        end
      end

      # Return zone price if defined otherwise return normal price
      def zone_price(country_id)
        # Find zone and check if zone price set for the product/variant
        zone = Country.find(country_id).zone
        zone_price = self.zone_prices.find_by_zone_id(zone.id) if zone && self.respond_to?('zone_prices')
        if zone_price
          # Zone price set us it
          amount = zone_price.price
        else
          amount = self.price
        end
      end

      # Method to default variant zone prices to the product zone prices
      def set_default_zone_prices(country_id)
        return if self.product.master.zone_prices.size <= 0 # No zone prices specified for product
        # Assign product zone prices as defaults
        self.product.master.zone_prices.each {|zp| self.zone_prices.build(zp.attributes)}
      end

    end
  end

end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
spree_zone_pricing-0.1.48 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.47 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.46 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.45 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.44 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.43 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.42 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.41 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.40 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.39 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.38 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.37 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.36 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.35 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.34 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.33 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.32 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.31 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.30 lib/spree/zone_pricing/variant.rb
spree_zone_pricing-0.1.29 lib/spree/zone_pricing/variant.rb