Sha256: d2ca5b90612c4133059ff1f930179ed684bcdffeb38e809ebaf44b51da7c7210

Contents?: true

Size: 1.93 KB

Versions: 109

Compression:

Stored size: 1.93 KB

Contents

begin
  north_america = Spree::Zone.find_by!(name: 'North America')
rescue ActiveRecord::RecordNotFound
  puts 'Couldn\'t find \'North America\' zone. Did you run `rake db:seed` first?'
  puts 'That task will set up the countries, states and zones required for Spree.'
  exit
end

europe_vat = Spree::Zone.find_by!(name: 'EU_VAT')
shipping_category = Spree::ShippingCategory.find_or_create_by!(name: 'Default')

shipping_methods = [
  {
    name: 'UPS Ground (USD)',
    zones: [north_america],
    display_on: 'both',
    shipping_categories: [shipping_category]
  },
  {
    name: 'UPS Two Day (USD)',
    zones: [north_america],
    display_on: 'both',
    shipping_categories: [shipping_category]
  },
  {
    name: 'UPS One Day (USD)',
    zones: [north_america],
    display_on: 'both',
    shipping_categories: [shipping_category]
  },
  {
    name: 'UPS Ground (EU)',
    zones: [europe_vat],
    display_on: 'both',
    shipping_categories: [shipping_category]
  },
  {
    name: 'UPS Ground (EUR)',
    zones: [europe_vat],
    display_on: 'both',
    shipping_categories: [shipping_category]
  }
]

shipping_methods.each do |attributes|
  Spree::ShippingMethod.where(name: attributes[:name]).first_or_create! do |shipping_method|
    shipping_method.calculator = Spree::Calculator::Shipping::FlatRate.create!
    shipping_method.zones = attributes[:zones]
    shipping_method.display_on = attributes[:display_on]
    shipping_method.shipping_categories = attributes[:shipping_categories]
  end
end

{
  'UPS Ground (USD)' => [5, 'USD'],
  'UPS Ground (EU)' => [5, 'USD'],
  'UPS One Day (USD)' => [15, 'USD'],
  'UPS Two Day (USD)' => [10, 'USD'],
  'UPS Ground (EUR)' => [8, 'EUR']
}.each do |shipping_method_name, (price, currency)|
  shipping_method = Spree::ShippingMethod.find_by!(name: shipping_method_name)
  shipping_method.calculator.preferences = {
    amount: price,
    currency: currency
  }
  shipping_method.calculator.save!
  shipping_method.save!
end

Version data entries

109 entries across 109 versions & 1 rubygems

Version Path
spree_sample-4.10.1 db/samples/shipping_methods.rb
spree_sample-4.10.0 db/samples/shipping_methods.rb
spree_sample-4.9.0 db/samples/shipping_methods.rb
spree_sample-4.8.3 db/samples/shipping_methods.rb
spree_sample-4.8.2 db/samples/shipping_methods.rb
spree_sample-4.7.3 db/samples/shipping_methods.rb
spree_sample-4.6.6 db/samples/shipping_methods.rb
spree_sample-4.5.5 db/samples/shipping_methods.rb
spree_sample-4.0.9 db/samples/shipping_methods.rb
spree_sample-4.1.15 db/samples/shipping_methods.rb
spree_sample-4.2.7 db/samples/shipping_methods.rb
spree_sample-4.3.3 db/samples/shipping_methods.rb
spree_sample-4.4.1 db/samples/shipping_methods.rb
spree_sample-4.5.4 db/samples/shipping_methods.rb
spree_sample-4.6.5 db/samples/shipping_methods.rb
spree_sample-4.7.1 db/samples/shipping_methods.rb
spree_sample-4.6.4 db/samples/shipping_methods.rb
spree_sample-4.7.0 db/samples/shipping_methods.rb
spree_sample-4.6.3 db/samples/shipping_methods.rb
spree_sample-4.6.2 db/samples/shipping_methods.rb