Class: Spree::Stock::Estimator

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

Defined Under Namespace

Classes: OrderRequired, ShipmentRequired

Instance Method Summary collapse

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”

Raises:



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

def shipping_rates(package, frontend_only = true)
  raise ShipmentRequired if package.shipment.nil?
  raise OrderRequired if package.shipment.order.nil?

  rates = calculate_shipping_rates(package)
  rates.select! { |rate| rate.shipping_method.available_to_users? } if frontend_only
  choose_default_shipping_rate(rates)
  Spree::Config.shipping_rate_sorter_class.new(rates).sort
end