Skip to content

Add new Shipping Calculator (Weight Rate) #1007

Closed
wants to merge 3 commits into from

4 participants

@kpitn
kpitn commented Jan 21, 2012

Many shipping cost are base on weight.

With this calculator you specify a rule :
Example : 1:4;2:5;3:10;4:14;10:30;20:70;40:120;100:150

If your weight is between 2 and 3, then your price is 10
If your weight is between 40 and 100, then your price is 150
If your weight is not in the rule, a default price is available
If your variant have no weight, a defaut weight is available

@radar radar commented on an outdated diff Jan 22, 2012
core/app/models/spree/calculator/weight_rate.rb
+ class Calculator::WeightRate < Calculator
+ preference :default_rule, :string
+ preference :default_weight, :decimal, :default => 1
+ preference :default_price, :decimal, :default => 0
+
+ def self.description
+ I18n.t(:weight_rate)
+ end
+
+ # as object we always get line items, as calculable we have Coupon, ShippingMethod
+ def compute(object)
+ line_items = object.is_a?(Order) ? object.line_items : object
+
+ if self.preferred_default_rule =~ /^(\d*\:\d*\;)*$|^(\d*\:\d*\;)*(\d*\:\d*)$/
+
+ total_weight = line_items.map{|li|
@radar
Spree Commerce member
radar added a note Jan 22, 2012

Here you could use sum in place of map, and then sum.

We'd also prefer that multi-lined blocks start with a do and end with an end, rather than the use of curly brackets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@kpitn
kpitn commented Jan 22, 2012

I listen to your advice and change syntax from {} to do / end block

@radar
Spree Commerce member
radar commented Jan 25, 2012

@schof: Do you have any feedback you would like to add about this feature request?

@kpitn
kpitn commented Jan 25, 2012

It's the only rule i use in french ecommerce website (oscommerce and magento)

@schof
Spree Commerce member
schof commented Jan 26, 2012

Seems useful but I need to look at it more carefully. We're in lockdown for changes to 1.0 right now so lets leave this alone for another few days until we have 1-0-stable branch and priority stuff closed out.

@GeekOnCoffee

Seems like a worthwhile feature, but would an approach such as the one taken by: https://github.com/spree/spree_volume_pricing make more sense? It seems to have a more straight forward definition pattern.

@radar
Spree Commerce member
radar commented Apr 18, 2012

Are we still going to do this for 1.1?

@schof
Spree Commerce member
schof commented Apr 19, 2012

Closing b/c this is out of date. I believe @cmar has something similar to what @GeekOnCoffee is suggesting that he came up with for the SaaS product. We should incorporate it into the core after 1.1 is released, etc.

@schof schof closed this Apr 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.