Skip to content

Shipping methods

Valentin Ballestrino edited this page Jan 26, 2016 · 7 revisions

To handle shipping fees in your app, there are existing shippings methods with associated calculators, but you can also create your own easily.

Once you have the shipping calculator, you only need to create a ShippingMethod in your database :

irb :001> Stall::ShippingMethod.create!(name: 'My shipping method', identifier: 'my_shipping_method')

Existing calculators

Creating your own

Create a Stall::Shipping::Calculator subclass in your app's lib/ folder, which defined two methods : #available_for?(address) and #price :

class MyShippingCalculator < Stall::Shipping::Calculator
  register :my_shipping_calculator

  def available_for?(address)
    address.country.in?(['FR', 'GB'])
  end

  def price
    cart.total_price > 100 ? 0 : 10
  end
end

Note : Inside the class you have access to the current cart, and the config property which is the Stall::ShippingMethod model instance.

Require it in the stall initializer :

require 'my-shipping-calculator`

Stall.configure do |config|
  # ...
end

Clone this wiki locally