Search cross-sell campaigns
Overview
Use the searchCrossSellCampaigns call to retrieve information on the cross-sell campaigns currently defined on your account.
Request parameters
Parameters |
Type |
Required |
Description |
---|---|---|---|
CampaignName |
String |
Optional |
The name of the campaign. |
Status |
String |
Optional |
The status of the campaign; can be ACTIVE/INACTIVE. |
Products |
Array of strings |
Optional |
Array of product codes to apply to this campaign. |
RecommendedProducts |
Array of strings |
Optional |
Array of product codes recommended to the shopper. |
StartDate |
String |
Optional |
The date when the cross-sell campaign starts, formatted as YYYY-MM-DD |
EndDate |
String |
Optional |
The date when the cross-sell campaign ends, formatted as YYYY-MM-DD |
Type | String | Optional | Can be MERCH/AFF. |
Pagination |
Object |
Optional |
|
Page |
Int |
Optional |
The page number of the results. |
Limit |
Int |
Optional |
The number of results per page. |
Request sample
<?php
declare(strict_types=1);
// Start clear CLI
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';
// End clear CLI
$executionStartTime = microtime(true);
class Configuration
{
public const MERCHANT_CODE = 'your_merchant_code';
public const MERCHANT_KEY = 'your_merchant_key';
public const URL = 'http://api.avangate.local/rpc/6.0';
}
class Client
{
private const LOGIN_METHOD = 'login';
private const SEARCH_CROSS_SELL_CAMPAIGNS = 'searchCrossSellCampaigns';
private int $calls = 1;
private function generateAuth(): array
{
$merchantCode = Configuration::MERCHANT_CODE;
$key = Configuration::MERCHANT_KEY;
$date = gmdate('Y-m-d H:i:s');
$string = strlen($merchantCode) . $merchantCode . strlen($date) . $date;
$hash = hash_hmac('md5', $string, $key);
return compact('merchantCode', 'date', 'hash');
}
public function login()
{
$payload = $this->generateAuth();
$response = $this->callForRpc(Configuration::URL, self::LOGIN_METHOD, array_values($payload));
return $response['result'];
}
private function callForRpc(string $url, string $action, ?array $payload = null)
{
$request = json_encode([
'jsonrpc' => '2.0',
'method' => $action,
'params' => $payload,
'id' => $this->calls++,
]);
$headers = [
'Content-Type: application/json',
'Accept: application/json',
'Cookie: XDEBUG_SESSION=PHPSTORM'
];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSLVERSION, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
$response = curl_exec($curl);
if (empty($response)) {
die('Server unavailable');
}
echo "\n\r Method " . $action . " result: \n\r";
echo $response . "\n\r";
return json_decode($response, true);
}
private function buildSearchParameters(): array
{
$searchOptions = new \stdClass();
$searchOptions->Status = ['ACTIVE']; // Optional | Ex: ['ACTIVE']
$searchOptions->Type = null; // Optional | Ex: 'MERCH'
$searchOptions->CampaignName = null; // Optional | Ex: 'UpdatedCampaign_4'
$searchOptions->Products = []; // Optional | Ex: ["a01", "a02"]
$searchOptions->RecommendedProducts = [];// Optional | Ex: ["a03"]
$searchOptions->StartDate = null; // Optional | Ex: 'YYYY-MM-DD'
$searchOptions->EndDate = null; // Optional | Ex: 'YYYY-MM-DD'
$searchOptions->Pagination = null; // Optional | Ex: {"Page": "1", "Limit": "10"}
return (array)$searchOptions;
}
public function searchCrossSellCampaigns($sessionId)
{
$response = $this->callForRpc(
Configuration::URL,
self::SEARCH_CROSS_SELL_CAMPAIGNS,
[$sessionId, $this->buildSearchParameters()]
);
return $response['result'];
}
}
$client = new Client();
$sessionId = $client->login();
$result = $client->searchCrossSellCampaigns($sessionId);
var_dump("SEARCH CROSS SELL CAMPAIGNS: \n\r", json_encode($result, JSON_PRETTY_PRINT));
$executionEndTime = microtime(true);
// The duration will be displayed in seconds and milliseconds.
$seconds = round($executionEndTime - $executionStartTime, 2);
// Print it out
echo "\n\rThis script took $seconds to execute.\n\r";
Response
Parameters | Type/Description |
---|---|
Items | An array of CrossSellCampaign Objects |
Pagination | Pagination Object with the following parameters: Page, Limit, Count |