Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add dynamic shipping methods to cart

  • Loading branch information...
commit 372ea52fbb893b6148716834e628b919b48b5cc8 1 parent fcfd143
Tim authored
Showing with 35 additions and 7 deletions.
  1. +35 −7 lib/google-checkout/cart.rb
View
42 lib/google-checkout/cart.rb
@@ -105,6 +105,31 @@ def flat_rate_shipping(frs_options)
@flat_rate_shipping = {:currency => 'USD'}.merge(frs_options)
end
+
+ # This method sets the shipping methods
+ # +options+ should be an array with hashes containing the following options:
+ # * method (must be: flat-rate shipping, )
+ # * name
+ # * price
+ # You may fill an some optional values as well:
+ # * currency (defaults to 'USD')
+ def shipping_methods(methods)
+ # We need to check that the necessary keys are in the hash,
+ # Otherwise the error will happen in the middle of to_xml,
+ # and the bug will be harder to track.
+
+ methods.each do |method|
+ missing_keys = [ :method, :name, :price ].select { |key|
+ !method.include? key
+ }
+ unless missing_keys.empty?
+ raise ArgumentError,
+ "Required keys missing: #{missing_keys.inspect}"
+ end
+ end
+
+ @shipping_methods = methods
+ end
def empty?
@contents.empty?
@@ -220,20 +245,23 @@ def to_xml
}
}
- # TODO Shipping calculations
- # These are currently hard-coded for PeepCode.
- # Does anyone care to send a patch to enhance
- # this for more flexibility?
xml.tag!('shipping-methods') {
- xml.tag!('pickup', :name =>'Digital Download') {
- xml.tag!('price', "0.00", :currency => currency)
- }
+ if @shipping_methods
+ @shipping_methods.each { |method|
+ xml.tag!("pickup", :name => method[:name]) {
+ xml.tag!('price', method[:price], :currency => method[:currency] || 'USD')
+ }
+ }
+ end
}
+
+
}
}
}
@xml.dup
end
+
# Generates the XML for the shipping cost, conditional on
# @flat_rate_shipping being set.
Please sign in to comment.
Something went wrong with that request. Please try again.