Create packages with active_shipping

240 views Asked by At

I use active_shipping gem for calculate shipping cost and I have some problem.My package can have several identical objects. I use PackageItem class for these but when I use find_rates method I have this error.

NoMethodError: undefined method 'inches' for #<ActiveMerchant::Shipping::PackageItem:0x007fec95f24610>

That is example how I use it:

origin = Location.new(country: 'US', zip: '91801')
dest = Location.new(country: 'US', zip: '90001')
packages = PackageItem('test', 32, 18, 15, units: :imperial)
carrier = USPS.new(login: 'LOGIN')
carrier.find_rates(origin, dest, packages)

This is initializer for PackageItem:

def initialize(name, grams_or_ounces, value, quantity, options = {})
    @name = name

    imperial = (options[:units] == :imperial) ||
      (grams_or_ounces.respond_to?(:unit) && m.unit.to_sym == :imperial)

    @unit_system = imperial ? :imperial : :metric

    @weight = attribute_from_metric_or_imperial(grams_or_ounces, Mass, :grams, :ounces)

    @value = Package.cents_from(value)
    @quantity = quantity > 0 ? quantity : 1

    @sku = options[:sku]
    @hs_code = options[:hs_code]
    @options = options
  end

Value is the item cost, thats how I understand this.

If using USPS carrier, the error can make in this place:

def self.size_code_for(package)
    if package.inches(:max) <= 12
      'REGULAR'
    else
      'LARGE'
    end
  end

Thanks!

1

There are 1 answers

1
tianon On BEST ANSWER

I think you need to be using the Package module instead of the PackageItem module (which doesn't appear to have quantity, so you'll have to duplicate your Package objects for higher quantities).

See also this note from the documentation of PackageItem: (http://www.rubydoc.info/github/Shopify/active_shipping/ActiveMerchant/Shipping/PackageItem)

This is only required for shipping methods (label creation) right now.