Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jsqu99/2973740 to your computer and use it in GitHub Desktop.
Save jsqu99/2973740 to your computer and use it in GitHub Desktop.
packages override for spree_active_shipping
# Override to exclude products with ships_free
def packages(order, line_items)
return nil if line_items.empty?
candidate_line_items = line_items - order.line_items_that_ship_free # never consider the free
# so if we have only ships_free products, actually let the items slip through so we can compute a real value
# we do this b/c we'll automatically provide a 'free shipping' option, and we'll also want to provide the 'non-free' options
# as well, per Chris' request.
if candidate_line_items.blank?
candidate_line_items = line_items
end
# I want to do this:
# return [] if candidate_line_items.empty?
# but we'll get an error of 'no packages', so really this is not the right place to capture the 'all ships free' condition
#
# We'll capture this at another level (the promotions).
#
# The code to prune ships_free products is still needed, and present, in order to remove them from consideration for orders that
# have non-ships-free products as well
# Per client packaging algorithm email, our threshold for going to a 2nd, 3rd, etc. package is going to be determined by weight (60lbs)
# group items together until we get to 60lbs
variant_groupings = group_individualized_line_items_by_weight(candidate_line_items)
# split each line item into 'qty == 1' components
# variant_groupings.size is # packages
# so here we are going to just get the largest item in each group and use that for dimension
packages = []
variant_groupings.each do |variants|
dimensions = variants.inject([1,1,1]) do |result, v|
# order by h,w,d
dims = [v.height || 1,v.width || 1,v.depth || 1]
dims.sort! {|x,y| y <=> x}
# return the largest dimsension for height & width, and sum depth
result[0] = [result[0], dims[0]].max
result[1] = [result[1], dims[1]].max
result[2] += dims[2]
result
end
weight = weight_of_variants(variants)
packages << ActiveMerchant::Shipping::Package.new(weight * Spree::ActiveShipping::Config[:unit_multiplier],
dimensions,
:units => Spree::ActiveShipping::Config[:units].to_sym)
end
packages
end
# This is some nasty code I wrote a few years ago. It yields multiple packages.
def packages(order)
pkgs=Array.new
# have we yet added the additional 2 inches to the depth of the box?
dim_arr=[]
order.line_items.each do |line_item|
line_item.quantity.times {|t| dim_arr<< (compute_dimensions line_item) }
end
#sort each individual array by size, descending e.g. [3,8,4] => [8,4,3]
dim_arr.each { |ra| ra.sort! {|x,y| y<=>x} }
# now sort the outter list by each elements [0] field, largest first, so that we have the largest frame as the height && weight below
dim_arr.sort! {|x,y| y[0]<=>x[0]}
# [[16, 12, 4], [14, 11, 4]]
# create one package for each group that will satisfy the oversize fedex limitations (formula is l+(2*w) +(2*h) <= 130)
# dim_arr.each do |da|
# end
# create one package for each group of 4 or less
total_qty=order.line_items.map(&:quantity).sum
num_pkgs=(total_qty / 4) + ((total_qty % 4) > 0?1:0)
pkgs=[]
num_pkgs.times do |t|
pkgs << dim_arr[(4*t) .. (4*t)+4] # take a chunk of 4 frames
end
#Create the Package(s)
packages=[]
pkgs.each do |pkg|
# each pkg contains an array of length 1..4
# the largest package is the first in the list, so only use the height, width of that one...and one 'grow' the depth
height, width, depth_sum = pkg[0][0] , pkg[0][1], pkg.inject(0) {|x,y| x+y[2]}
fedex_oversize_sum = height + 12 + (2*(width+12)) + (2*(depth_sum+2))
Rails.logger.error("CHEAT: orig fedex_oversize_sum: #{fedex_oversize_sum}")
# let's fudge the numbers just a tad if we need to in order to avoid the oversize charge
if (fedex_oversize_sum > 130 && fedex_oversize_sum < 160)
Rails.logger.error("CHEAT: we just shaved the numbers from #{width} - #{height} - #{depth_sum}")
amount_to_remove = fedex_oversize_sum-130
((amount_to_remove / 5) + 1).times do |t| # the '5' is b/c width & depth get mult by 2 in fedex's calc
height = height -1
width = width - 1
depth_sum = depth_sum - 1
end
end
#Weight..assume 1 lb per frame
weight=Spree::ActiveShipping::Config[:unit_multiplier] # 1x16=16
# the +(1)2 below is for the outter-most padding, irrespective of # of frames
packages<< ActiveMerchant::Shipping::Package.new(weight, [height+12, width +12, depth_sum+2], :units => Spree::ActiveShipping::Config[:units].to_sym)
end
packages
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment