CSV: Creating Coupons with CSV

Getting Ready

Follow the instructions from the first two sections of the Creating Your First User article to setup and configure the Mambo PHP SDK.

The Script

Below you will find a script that loads coupons into Mambo based on a CSV file. The CSV file can also be found below so that you can follow the same structure for your data. Note that both files are also available for download.

In order to use the script you will need to take the following steps:

  • Configure the fields in the script that require your input (see below)
  • Add your coupons to the CSV file
  • Setup the Mambo PHP SDK as explained in Creating Your First User

There are 4 fields in the script that require your input:

Once everything is configured you can run your CSV loader with the following command:

php coupon_loader.php sample_coupon_data.csv

The coupon_loader.php script:


// Import the library files

// --------------- CONFIGURATION START ----------------

// Configure server and security variables

// --------------- CONFIGURATION END ----------------

// Initialise the clients credentials and end point URL, this only needs to be done once
MamboClient::setCredentials( $publicKey, $privateKey );
MamboClient::setEndPointBaseUrl( $serverUrl );

// Use default timezone to parse dates

// Check if a CSV file was supplied
if( $argc != 2 ) {
exit("Please provide a CSV file name as an argument i.e. php coupon_loader my_file.csv");

// Open the CSV file
$csvFileName = $argv[1];
$handle = fopen( $csvFileName, "r" );

if( $handle === FALSE ) {
exit("Failed to open the CSV file: " + $csvFileName);

// Process the CSV file
$row = 0;

while( ( $data = fgetcsv( $handle, 1000, "," ) ) !== FALSE )

debugLog( "---------------------------------" );
debugLog( "Parsing row number: " . $row );

if( $row == 1 ) {
debugLog( "Head row: skipping" );

createCoupon( $data );

debugLog( "Completed row number: " . $row );

// Close the CSV file
fclose( $handle );

// --------------- Helper Functions ----------------

// Try to create the coupon unless they already exist
function createCoupon( $data )
global $siteUrl;

debugLog( "Creating coupon: " . $data[0] );

$requestData = createrequestData( $data );
$coupon = MamboCouponsService::create( $siteUrl, $requestData );
validateCouponCreation( $data[0], $coupon );

function createrequestData( $data )
$requestData = new CouponRequestData();
$requestData->setName( $data[0] );
$requestData->setActive( $data[1] );
$requestData->setCode( $data[2] );
$requestData->setUsage( $data[3] );
$requestData->setType( $data[4] );
$requestData->setAmount( $data[5] );
$requestData->setCustomPrize( $data[6] );
$requestData->setHelpMessage( $data[7] );
$requestData->setExpiration( new NeverRecurrence() );

$scarcity = new Scarcity();
$scarcity->setLimit( $data[8] );
$requestData->setScarcity( $scarcity );

if( $data[9] !== "" ) {
$point = new PointStore();
$point->setPointId( $data[9] ); // Required
$point->setPoints( $data[10] ); // Required
$requestData->setPointsToBuy( $point );

return $requestData;

function validateCouponCreation( $name, $object ) {
if( isset( $object->error ) ) {
if( strcmp( $object->error->type, "DuplicateException" ) === 0 ) {
debugLog( "Existing object on server: " . $name );
} else {
debugLog( "An unexpected error occurred! Dumping output and terminating." );
var_dump( $object );

// Basic output to the terminal
function debugLog( $text ) {
echo $text . "\n";

The sample_coupon_data.csv:

Time Off Work,true,TIMEOFF,single,custom,,Get Half a Day Off,Work more and rest more,1,5512e29c574df40a2527565b,120
1-on-1 with CEO,true,CEOTALKS,single,custom,,1-on-1 with CEO,Top employees get a special benefit,1,5512e29c574df40a2527565b,1200

The fields in the CSV file represent:

  • Name: the name of the coupon we are creating.
  • Active: whether the coupon should be active or not. Valid values include: true / false
  • Code: the code that uniquely identifies this coupon in the system. This must be a unique value. We recommend using something descriptive of the reward.
  • Usage: whether the coupon can be used once or many times. Valid values include: single / multi
  • Type: the type of coupon we want to create. Valid values include: percent / fixed / ship / custom
  • Amount: if the type of the coupon is percent or fixed, then this must contain the monetary amount for the fixed coupon or the percentage amount for the percent coupon. In the case of ship or custom coupons, this field should be left empty.
  • Custom: if the type of the coupon is custom then this must contain the custom message associated to the coupon e.g. Buy 1 Get 1 Free
  • Help Message: helpful text the user can read to better understand what the coupon is or what are the rules associated to it.
  • Limited Series: this can be used to limit how many of these coupons are available. Leave this empty if there is no limit. Note that this limit is global so it means that only X coupons will be distributed in total.
  • Exchange Point Id: if the coupon can be bought with points, then this should contain the ID of the point we want to use
  • Exchange Point Amount: if the coupon can be bought with points, then this should contain the amount of points required to use this coupon

Download the files below:


Article is closed for comments.