Retrieve cross-sell campaigns
Overview
Use the searchCrossSellCampaigns method to extract information about the cross-sell campaigns you configured.
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. |
Response
Parameters | Type/Description |
---|---|
Items | An array of CrossSellCampaign Objects |
Pagination | Pagination Object with the following parameters: Page, Limit, Count |
Request
<?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 GET_CROSS_SELL_CAMPAIGN = 'getCrossSellCampaign';
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);
}
public function getCrossSellCampaign($sessionId, $crossSellCampaignCode)
{
$response = $this->callForRpc(
Configuration::URL,
self::GET_CROSS_SELL_CAMPAIGN,
[$sessionId, $crossSellCampaignCode]
);
return $response['result'];
}
}
$crossSellCampaignCode = '2Xrl83J0k+qOr3W1ceTwZnHHr30=';
$client = new Client();
$sessionId = $client->login();
$result = $client->getCrossSellCampaign($sessionId, $crossSellCampaignCode);
var_dump("GET CROSS SELL CAMPAIGN BY CODE: \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";