Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added dynamic shipping methods to cart #5

Merged
merged 1 commit into from

2 participants

@thogg4

No description provided.

@topfunky topfunky merged commit 2bd124a into topfunky:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 19, 2012
  1. add dynamic shipping methods to cart

    Tim authored
This page is out of date. Refresh to see the latest.
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.
Something went wrong with that request. Please try again.