Skip to content

Instantly share code, notes, and snippets.

@rapind
Forked from resistorsoftware/coupons
Created September 27, 2011 19:55
Show Gist options
  • Save rapind/1246051 to your computer and use it in GitHub Desktop.
Save rapind/1246051 to your computer and use it in GitHub Desktop.
Load Groupon Codes into Shopify Discounts
# 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.send("discount[code]=", code)
f.send("discount[value]=", DISCOUNT)
f.send("discount[usage_limit]=", "1")
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
@scofennell
Copy link

Hi,

This looks really nice. Could you help provide some context though? Where does this code go?

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