Skip to content

Instantly share code, notes, and snippets.

@Soleone
Forked from rapind/coupons
Created November 21, 2011 23:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Soleone/1384296 to your computer and use it in GitHub Desktop.
Save Soleone/1384296 to your computer and use it in GitHub Desktop.
Load Groupon Codes into Shopify Discounts (fixed based on new AJAX form in admin))
# http://forums.shopify.com/categories/6/posts/42926
# This is a fairly simple mechanize script to create a few hundred or so shopify discounts from a CSV of groupon codes.
# You'll want to change all of the "changeme" values to your own.
# The script expects there to be a groupon_codes.csv in the same directory.
#
# The groupons_code.csv should look like this:
# 8m7677
# 8m6749
# 8m5398
# 8m7699
# 8m8885
# 8m788
# etc.
require 'rubygems'
require 'mechanize'
require 'csv'
BASE_URL = 'https://changeme.myshopify.com/admin'
LOGIN_PATH = '/auth/login'
USER = 'chamgeme'
PASSWORD = 'chamgeme'
DISCOUNT = "chamgeme"
@agent = Mechanize.new
@groupon_codes = []
def login
page = @agent.get(BASE_URL + LOGIN_PATH)
form = page.forms.first
form.login = USER
form.password = PASSWORD
@agent.submit(page.forms.first)
end
def load_codes
@groupon_codes = CSV.read("groupon_codes.csv").flatten
end
def create_discount(page, code)
puts "Create discount for code: #{code}..."
# You may want to have some error handling in case you have connection issues. Worked fine for me with 500+ codes though.
page.form_with(:action => "#{BASE_URL}/discounts") do |f|
f['discount[code]'] = code
f["discount[usage_limit]"] = '1'
# Set both the visible and hidden textfield to ensure the value gets sent
discount_value_fields = f.fields_with(:id => 'discount_value')
discount_value_fields.each do |textfield|
textfield.value = DISCOUNT
end
end.click_button
end
load_codes
login
@agent.get(BASE_URL + '/marketing') do |page|
@groupon_codes.each do |code|
create_discount(page, code)
end
end
@bungaloo
Copy link

Does anyone know if this tool allows you to leave shipping and tax applicable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment