Class: Spree::Stock::Estimator

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/stock/estimator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ Estimator

Returns a new instance of Estimator.

Parameters:

  • order (Spree::Order)

    the order whose shipping rates to estimate



7
8
9
10
# File 'app/models/spree/stock/estimator.rb', line 7

def initialize(order)
  @order = order
  @currency = order.currency
end

Instance Attribute Details

#currencyObject (readonly)

Returns the value of attribute currency.



4
5
6
# File 'app/models/spree/stock/estimator.rb', line 4

def currency
  @currency
end

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'app/models/spree/stock/estimator.rb', line 4

def order
  @order
end

Instance Method Details

#shipping_rates(package, frontend_only = true) ⇒ Array<Spree::ShippingRate>

Estimate the shipping rates for a package.

Parameters:

  • package (Spree::Stock::Package)

    the package to be shipped

  • frontend_only (Boolean) (defaults to: true)

    restricts the shipping methods to only those marked frontend if truthy

Returns:

  • (Array<Spree::ShippingRate>)

    the shipping rates sorted by descending cost, with the least costly marked “selected”



19
20
21
22
23
24
# File 'app/models/spree/stock/estimator.rb', line 19

def shipping_rates(package, frontend_only = true)
  rates = calculate_shipping_rates(package)
  rates.select! { |rate| rate.shipping_method.frontend? } if frontend_only
  choose_default_shipping_rate(rates)
  sort_shipping_rates(rates)
end