Skip to content

Instantly share code, notes, and snippets.

@pollingj
Created September 26, 2012 08:18
Show Gist options
  • Save pollingj/3786746 to your computer and use it in GitHub Desktop.
Save pollingj/3786746 to your computer and use it in GitHub Desktop.
Splitting packages in Spree Active Shipping
def packages(order)
split_packages = Array.new
number_of_packages = 1
multiplier = Spree::ActiveShipping::Config[:unit_multiplier]
default_weight = Spree::ActiveShipping::Config[:default_weight]
weight = order.line_items.inject(0) do |weight, line_item|
item_weight = line_item.variant.weight.present? ? line_item.variant.weight : default_weight
weight + (line_item.quantity * item_weight * multiplier)
end
if weight > MAX_WEIGHT_PER_PACKAGE
number_of_packages = (weight.to_i / MAX_WEIGHT_PER_PACKAGE)
number_of_packages += 1 if weight % MAX_WEIGHT_PER_PACKAGE > 0
end
packages_weight = weight
(1..number_of_packages).each do
if packages_weight > MAX_WEIGHT_PER_PACKAGE
package_weight = MAX_WEIGHT_PER_PACKAGE
packages_weight -= MAX_WEIGHT_PER_PACKAGE
else
package_weight = packages_weight
end
puts package_weight
Spree::ActiveShipping::Config[:units].to_sym
split_packages << Package.new(package_weight, [], :units => Spree::ActiveShipping::Config[:units].to_sym)
end
split_packages
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment