Skip to content

Instantly share code, notes, and snippets.

@noxious
Forked from tomash/pulp_rate.rb
Created April 28, 2011 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noxious/945795 to your computer and use it in GitHub Desktop.
Save noxious/945795 to your computer and use it in GitHub Desktop.
Spree shipping cost calculator
class Calculator::PulpRate < Calculator
preference :category1_rate, :decimal, :default => 0
preference :category2_rate, :decimal, :default => 0
preference :category3_rate, :decimal, :default => 0
preference :category4_rate, :decimal, :default => 0
preference :category5_rate, :decimal, :default => 0
WEIGHT_RANGES = {
0..350 => :preferred_category1_rate,
351..500 => :preferred_category2_rate,
501..1000 => :preferred_category3_rate,
1001..2000 => :preferred_category4_rate
}
def self.description
I18n.t("pulp_rate")
end
def self.available?(object)
true
end
def self.register
super
ShippingMethod.register_calculator(self)
end
def compute(object)
sum = 0
weight = 0.1 # 100g for packaging weight
total = 0.0
possible_free_shipping = true
object.line_items.each do |li|
weight += (li.quantity * li.variant.weight.to_f)
total += (li.quantity * li.price)
# book means no free shipping
possible_free_shipping = false if(li.variant.sku == "PPMR01")
end
#free shipping for orders above 120 EUR
return 0.0 if(possible_free_shipping && (total > 120.0))
WEIGHT_RANGES.each do |wr, pref|
return self.send(pref) if(wr.include?(weight*1000))
end
return self.preferred_category5_rate
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment