How to calculate shipping costs for the marketplace tutorial?

Hello, I have successfully completed the marketplace tutorial, and now I would like to add a feature where the shipping costs is calculated. Recently, I just discovered the "active_shipping" gem, and I think using it will help simplify the task of calculating the shipping. However, I am a bit perplexed as to where to start when building out this feature. I have already added the "active_shipping" gem to my Gemfile, so I just need some ideas on what are some possible ways to calculate shipping. On a side note, I only plan on shipping via USPS, and any and all help on this subject would greatly be appreciated.

Posted over 4 years ago by Chris
Posted over 4 years ago by Alex Yang

Have you already looked over the documentation? The examples they provide seem pretty straightforward. To check shipping cost via USPS, you should use code like the below:

require 'active_shipping'

usps = 'developer-key') # You will probably need to register to get a developer-key
response = usps.find_rates( 'US',
state: 'CA',
city: 'Beverly Hills',
zip: '90210'), 'CA',
province: 'ON',
city: 'Ottawa',
postal_code: 'K1P 1J1'),
[, [93,10], cylinder: true)])

usps_rates = response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}

Posted over 4 years ago by Chris

@Alex, thanks again for the reply. I think I am going to try and accomplish this using JavaScript.

Posted over 4 years ago by Alex Yang

Great - let me know how it goes!