How to load (display) coupons via php API?

The following code shows, how you can load valid and assigned coupons via php API.
 
<?php
		
//your PapApi.class.php file can be downloaded in the merchant panel at: 
//Tools>Integration>API Integration>Download PAP API

//this include below assumes the PapApi.class.php is in the same dir as this script
include ("PapApi.class.php"); 

//change localhost/pap to the path of your Post Affiliate Pro installation
$session = new Pap_Api_Session("https://localhost/pap/scripts/server.php");

//login as merchant (in PAN as Network-Owner) using merchant panel login credentials
if(!$session->login("merchant@example.com","demo")) {
  die("Cannot login. Message: ". $session->getMessage());
}

$request = new Gpf_Rpc_GridRequest("Pap_Features_Coupon_CouponsGrid", "getRows", $session);

//9abc240b is the ID of the coupon banner at Banners > Banners manager. 
//If the ID is not displayed, enable it in the "Edit View" section.
$request->addFilter("bannerid", Gpf_Data_Filter::EQUALS, '9abc240b'); 

try  {
  $request->sendNow();
} catch(Exception $e){
  die("API call error: " . $e->getMessage());
}

// request was successful, get the grid result
$grid = $request->getGrid();

// get recordset from the grid
$recordset = $grid->getRecordset();

$todayDate = date("Y-m-d");

// iterate through the records
foreach($recordset as $rec) {
	$ValidFromDate = $rec->get('valid_from');
	$from = explode(" ",$ValidFromDate);
			
	$ValidToDate = $rec->get('valid_to');
	$to = explode(" ",$ValidToDate);
	
	//load only approved and valid coupons which are assigned to some affiliate
	if ((($rec->get('userid'))!=NULL) && (($rec->get('rstatus'))=='A') && ($from[0]>=$todayDate) && ($to[0]<=$todayDate) ) {
	//print_r($rec); //here you can get a list of all of the available data. 
		echo '<br> Coupon code: '.$rec->get('couponcode').' Name: ' .$rec->get('firstname') . ' ' .$rec->get('lastname') . '<br>';
	}
}

//in case there are more then 30 records,lets load the other records in cycles
$totalRecords = $grid->getTotalCount();
$maxRecords = $recordset->getSize();

if ($maxRecords != 0) {
	$cycles = ceil($totalRecords / $maxRecords);
	for($i=1; $i<$cycles; $i++) {
		$request->setLimit($i * $maxRecords, $maxRecords);			
		$request->sendNow();
		$recordset = $request->getGrid()->getRecordset();
		// iterate through the records
		foreach($recordset as $rec) {
			$ValidFromDate = $rec->get('valid_from');
			$from = explode(" ",$ValidFromDate);
						
			$ValidToDate = $rec->get('valid_to');
			$to = explode(" ",$ValidToDate);
				
			//load only approved and valid coupons which are assigned to some affiliate
			if ((($rec->get('userid'))!= NULL) && (($rec->get('rstatus'))=='A') && ($from[0]<=$todayDate) && ($to[0]>=$todayDate) && ($rec->get('limit_use') == '0' || ($rec->get('usecount')<$rec->get('limit_use')))) {
			//print_r($rec); //here you can get a list of all of the available data. 
			echo '<br> Coupon code: '.$rec->get('couponcode').' Name: ' .$rec->get('firstname') . ' ' .$rec->get('lastname') .  '<br>';
			}
		}
	}
}
	
?>
 
 
×