Create fixed discount
Last updated: 29-Sep-2021
Rate this article:
Overview
Use setPromotionDiscount via SOAP API 4.0 to set a fixed promotion discount.
Parameters
Parameter | Type/Description | |||
---|---|---|---|---|
sessionID |
Required (string) |
|||
|
Output of the Login method. |
|||
promotionCode |
Required (string) |
|||
|
The code corresponding to the promotion that you want to set the discount for. |
|||
promotionDiscount |
Required(Object) |
|||
|
Type |
Required (String) |
||
|
|
Discount type:
|
||
|
Value / Values |
Required (Int / Array of objects) |
||
|
|
|
||
|
|
Value |
Required (Object) |
|
|
|
|
Currency |
Required (String) |
|
|
|
|
Discount currency ISO code (ISO 4217). |
|
|
|
Amount |
Required (Int) |
|
|
|
|
Discount amount in corresponding currency. |
|
DefaultCurrency |
Required (String) |
||
|
|
Default discount currency ISO code (ISO 4217). |
Response
Parameter | Type/Description |
---|---|
promotionDiscount | Object |
Request
<?php
class Client
{
protected static $merchantCode;
protected static $loginDate;
protected static $hash;
protected static $baseUrl;
protected static $callCount = 0;
protected static $sessionId = '';
protected static $client;
public static function setCredentials($code, $key)
{
static::$merchantCode = $code;
static::$loginDate = gmdate('Y-m-d H:i:s');
static::$hash = hash_hmac('md5', strlen($code) . $code . strlen(static::$loginDate) . static::$loginDate, $key);
static::$sessionId = static::login();
}
public static function setBaseUrl($url)
{
static::$baseUrl = $url;
}
public static function login()
{
$client = static::getClient();
return $client->login(static::$merchantCode, static::$loginDate, static::$hash);
}
public static function __callStatic($name, $arguments = array())
{
$client = static::getClient();
array_unshift($arguments, static::$sessionId);
$response = call_user_func_array(array($client, $name), $arguments);
return $response;
}
protected static function getClient()
{
$opts = array(
'http'=> ['user_agent' => 'PHPSoapClient'],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
);
if (null === static::$client) {
static::$client = new \SoapClient(static::$baseUrl . '?wsdl', [
'location' => static::$baseUrl,
'cache_wsdl' => WSDL_CACHE_NONE,
'stream_context' => stream_context_create($opts),
]);
}
return static::$client;
}
}
Client::setBaseUrl('https://api.avangate.com/soap/3.1/');
Client::setCredentials('YOUR_MERCHANT_CODE', 'YOUR_SECRET_KEY');
Client::login();
// define first discount
$value1 = new stdClass;
$value1->Currency = 'USD';
$value1->Amount = 20.5;
// define second discount
$value2 = new stdClass;
$value2->Currency = 'EUR';
$value2->Amount = 18.8;
// create final discount object, with the values added above
$discountObj = new stdClass;
$discountObj->Type = 'FIXED';
$discountObj->DefaultCurrency = 'USD';
$discountObj->Values = [$value1, $value2];
$promotionCode = 'YOUR_PROMO_CODE'; // code of the promotion that you want to update
$response = Client::setPromotionDiscount($promotionCode,$discountObj); // Set the promotion discount to the existing promotion
var_dump($response);
Rate this article: