-
Notifications
You must be signed in to change notification settings - Fork 7
Shipping methods
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')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
endNote : Inside the class you have access to the current
cart, and theconfigproperty which is theStall::ShippingMethodmodel instance.
Require it in the stall initializer :
require 'my-shipping-calculator`
Stall.configure do |config|
# ...
end
Stall comes from an abstract calculator class that handles a specific CSV format to store shipping fees depending on total order weight (rows) and target country (columns).
The CSV should contain comma separated ISO-3166-1 alpha-2 country codes (as provided by the country_select gem for example) in each top-row cells and max order weight in the first column cells.
The CSV will look like the following :
| "FR,GB,DE" | "US,MX" | |
|---|---|---|
| 1 | 5 | 10 |
| 2 | 10 | 20 |
| 5 | 15 | 30 |
| 10 | 30 | 40 |
| 100 | 40 | 100 |
Your calculator should subclass Stall::Shipping::CountryWeightTableCaclulator and implement at least the #load_data method returning a string with a CSV contents. The calculator will parse the CSV with ruby's standard csv lib.
class MyCSVCalculator < Stall::Shipping::CountryWeightTableCaclulator
def load_data
File.read(Rails.root.join('vendor/shipping_prices.csv'))
end
endAny country that is not in the list will not be available for this shipping method.