public
Created

Create N unique Eventbrite discount codes for an event

  • Download Gist
discount_upload.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?php
// load the API Client library
// https://github.com/ryanjarvinen/eventbrite.php
include "Eventbrite.php";
 
// -------------- APPLICATION CONFIG ----------------- //
// Eventbrite API key (REQUIRED)
// http://www.eventbrite.com/api/key/
$app_key = 'YOUR_APP_KEY_HERE';
// Eventbrite user_key, required for reading/writing data
// http://www.eventbrite.com/userkeyapi
$user_key = 'YOUR_USER_KEY_HERE';
 
// PLEASE RUN A TEST BEFORE CREATING YOUR DISCOUNT CODES
// "$test_mode = true" will do a test-run, new discount codes will be
// printed to the screen without being created.
// Running the script with "$test_mode = false" will create your discounts
$test_mode = true;
 
// DEFAULT DISCOUNT SETTINGS
// See http://developer.eventbrite.com/doc/discount/discount_new/
// for more information about these parameters
$discount_options = array(
// The event_id that the discount applies toward
'event_id' => 1905947770,
// amount OR percent off
'amount_off' => 25,
// ticket_ids that the discount will apply toward
'tickets' => '10206191,10206201,10206209,10207185',
// quantity of discounts available
'quantity_available' => '2'
);
 
// Generate random codes, or read codes in from a csv file?
// for random codes, set "$csv_filename = false;" and adjust the variables in the next section,
// to read in discount codes from a file, supply the filename here:
$csv_filename = false;
 
// VARIABLES FOR GENERATING N UNIQUE RANDOM DISCOUNT CODES
// (skip this section and supply a filename above if you want codes from a csv file)
$total_discount_codes = 1000;
$discount_prefix = 'DISC-';
$initial_offset = 5555;
$discount_increment = 667;
 
// ------------------ End of Config ------------------ //
 
// create, or simulate the creation of discount codes:
function create_discount( $eb_client, $discount_options, $test_mode ){
print('adding code: ' . $discount_options['code'] . "\n");
 
//create your discount codes -
// BE CAREFUL!
if($test_mode != true){
$resp = $eb_client->discount_new($discount_options);
var_dump($resp);
}
}
 
// Initialize the API client
$eb_client = new Eventbrite( $app_key, $user_key );
 
// Generate random codes based on a pattern, or read codes in from a CSV file:
if( $csv_filename ){
if (($handle = fopen($csv_filename, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// Read each discount code number in from a csv file:
$discount_options['code'] = $data[0];
create_discount($eb_client, $discount_options, $test_mode);
}
fclose($handle);
}
}else{
for ( $i = $initial_offset; $i < $total_discount_codes + $initial_offset; $i++ ) {
// generate a semi-unique discount code -
// you may want to tweak the $initial_offset or $base_discount_increment
// to make sure that your pattern is less guessable
$random_num = $i * $discount_increment;
//generate discount_new API params
$discount_options['code'] = $discount_prefix . $random_num;
create_discount($eb_client, $discount_options, $test_mode);
}
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.