Skip to content

Instantly share code, notes, and snippets.

@csaunders
Created January 19, 2012 20:27
Show Gist options
  • Save csaunders/1642383 to your computer and use it in GitHub Desktop.
Save csaunders/1642383 to your computer and use it in GitHub Desktop.
prod = ShopifyAPI::Product.find(6)
before = prod.variants.collect{|v| "#{v.position} - #{v.option1}"}
prod.variants[0].position = 3
prod.variants[2].position = 1
prod.variants.collect(&:save)
prod = ShopifyAPI::Product.find(6)
after = prod.variants.collect{|v| "#{v.position} - #{v.option1}"}
puts "Before: #{before}"
puts "After: #{after}"
# Expected output
# ["1 - oranges", "2 - apples", "3 - bananas"]
# ["1 - bananas", "2 - apples", "3 - oranges"]
# EDIT: Perform the replacement in a single API call
prod = ShopifyAPI::Product.find(6)
before = prod.variants.collect{|v| "#{v.position} - #{v.option1}"}
prod.variants = prod.variants.reverse
prod.save
prod = ShopifyAPI::Product.find(6)
after = prod.variants.collect{|v| "#{v.position} - #{v.option1}"}
puts "Before: #{before}"
puts "After: #{after}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment