Create promotion
Overview
Use the addPromotion method to create a new promotion.
Parameters
Parameters |
Type/Description |
|||||||
---|---|---|---|---|---|---|---|---|
sessionID |
Required (String) |
|||||||
|
Output of the Login method. |
|||||||
Promotion |
Object |
|||||||
|
Name |
Optional(String) |
||||||
|
|
Promotion name |
||||||
|
Description |
Optional(String) |
||||||
|
|
Promotion description |
||||||
|
StartDate |
Optional(String) |
||||||
|
|
Starting date. The date when you set the promotion to start. Is NULL for promotions that start immediately after they're created. Format: Y-m-d |
||||||
|
EndDate |
Optional(String) |
||||||
|
|
Ending date. The date when you set the promotion to end. Is NULL for promotions that you want active indefinitely. Format: Y-m-d |
||||||
|
MaximumOrdersNumber |
Optional(Int) |
||||||
|
|
Avangate stops offering the discount when the promotion reaches the maximum number of orders. Can be NULL if you want the promotion to apply to an unlimited number of orders. |
||||||
|
MaximumQuantity |
Optional(Int) |
||||||
|
|
Discount only applies to a specific number of product, smaller than the maximum quantity you defined. Can be NULL if you want the promotion to apply to an unlimited number units. Any extra quantity added to the cart will be sold at full price. |
||||||
|
InstantDiscount |
Optional(Boolean) |
||||||
|
|
Selecting the instant discount option will auto-apply the discount for ALL the selected products for all shoppers, without the need to enter the discount coupon. |
||||||
Coupon |
Optional(Object) |
|||||||
|
Type |
Optional(String) |
||||||
|
|
|
||||||
|
Code/Codes |
Optional(String/Array) |
||||||
|
|
Varies according to type. Send:
|
||||||
Enabled |
Optional(Boolean) |
|||||||
|
TRUE FALSE |
|||||||
ChannelType |
Optional(String) |
|||||||
|
Possible values:
|
|||||||
Type |
Optional(String) |
|||||||
|
REGULAR |
|||||||
Discount |
Optional(Object) |
|||||||
|
Type |
Optional(String) |
||||||
|
|
Discount type:
|
||||||
|
Value / Values |
Optional (Int / Array of objects) |
||||||
|
|
|
||||||
|
|
Value |
Optional(Object) |
|||||
|
|
Currency |
Optional (String) |
|||||
|
|
|
|
ISO currency code |
||||
|
|
Amount |
Optional (Int) |
|||||
|
|
|
|
Discount amount in corresponding currency. |
||||
|
DefaultCurrency |
Optional(String) |
||||||
|
|
ISO code |
||||||
Products |
Optional(Object) |
|||||||
|
Code |
Optional(Int) |
||||||
|
|
Unique product code that you control. |
||||||
|
PricingConfigurationCode |
Optional(String) |
||||||
|
|
Unique system generated pricing configuration code. |
||||||
|
PricingOptionCodes |
Array of strings |
||||||
|
|
Array of pricing option codes controlled by you. |
||||||
PriceThreshold |
Object |
|||||||
|
Limits discount use only when total order value (taxes included) exceeds the threshold you configure. |
|||||||
|
Amount |
Decimal |
||||||
|
|
The minimum threshold you defined for the default currency. |
||||||
|
Currency |
String |
||||||
|
|
Currency code available for the default currency of custom threshold settings. |
||||||
Translations |
Optional(Array of objects) |
|||||||
|
PromotionTranslation |
Optional(Object) |
||||||
|
|
Name |
Optional(String) |
|||||
|
|
|
Localized name corresponding to translated language. Name: Le product Language: FR |
|||||
|
|
Language |
Optional(String) |
|||||
|
|
|
||||||
Sources |
StringArray |
|||||||
|
Array of strings defining the sale source through the SRC parameter. |
|||||||
PublishToAffiliatesNetwork |
Optional(Int) |
|||||||
|
||||||||
ApplyRecurring |
Optional(String) |
|||||||
|
|
|||||||
RecurringChargesNumber |
Optional(Int) |
|||||||
|
Offer discounts for a number of recurring billing charges beyond the initial purchase. |
Response
Parameters | Type/Description |
---|---|
Promotion | Object |
Request
<?php
function callRPC($Request, $host, $Debug = true) {
$curl = curl_init($host);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSLVERSION, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
$RequestString = json_encode($Request);
curl_setopt($curl, CURLOPT_POSTFIELDS, $RequestString);
if ($Debug) {
$RequestString;
}
$ResponseString = curl_exec($curl);
if ($Debug) {
$ResponseString;
}
if (!empty($ResponseString)) {
var_dump($ResponseString);
$Response = json_decode($ResponseString);
if (isset($Response->result)) {
return $Response->result;
}
if (!is_null($Response->error)) {
var_dump($Request->method, $Response->error);
}
} else {
return null;
}
}
$host = 'https://api.avangate.com/rpc/3.1/';
$merchantCode = "YOUR_MERCHANT_CODE"; // your account's merchant code available in the 'System settings' area of the cPanel: https://secure.avangate.com/cpanel/account_settings.php
$key = "YOUR_SECRET_KEY"; // your account's secret key available in the 'System settings' area of the cPanel: https://secure.avangate.com/cpanel/account_settings.php
$string = strlen($merchantCode) . $merchantCode . strlen(gmdate('Y-m-d H:i:s')) . gmdate('Y-m-d H:i:s');
$hash = hash_hmac('md5', $string, $key);
$i = 1;
$jsonRpcRequest = new stdClass();
$jsonRpcRequest->jsonrpc = '2.0';
$jsonRpcRequest->method = 'login';
$jsonRpcRequest->params = array($merchantCode, gmdate('Y-m-d H:i:s'), $hash);
$jsonRpcRequest->id = $i++;
$sessionID = callRPC($jsonRpcRequest, $host);
promotionObject = new stdClass;
$promotionObject->Name = 'YOUR_PROMOTION_TITLE';
$promotionObject->Description = 'YOUR_PROMOTION_DESCRIPTION';
$promotionObject->StartDate = date('Y-m-d', strtotime('1 month ago'));
$promotionObject->EndDate = date('Y-m-d', strtotime('+1 month'));
$promotionObject->Type = 'REGULAR';
$promotionObject->Enabled = 1;
$promotionObject->MaximumOrdersNumber = 0;
$promotionObject->MaximumQuantity = 0;
$promotionObject->InstantDiscount = false;
$promotionObject->ChannelType = 'ECOMMERCE';
$promotionObject->ApplyRecurring = 'NONE';
$promotionObject->RecurringChargesNumber = 3;
$promotionObject->PublishToAffiliatesNetwork = 0;
$promotionObject->InstantDiscount = 0;
$couponObject = new stdClass;
$couponObject->Type = 'SINGLE';
$couponObject->Code = 'single_code';
$promotionObject->Coupon = $couponObject;
$discountObj = new stdClass;
$discountObj->Type = 'PERCENT';
$discountObj->Value = 30;
$promotionObject->Discount = $discountObj;
$promotionObject->Products = [$productsObj1, $productsObj2, $productsObj3];
$promotionObject->Translations = [$translation1, $translation2];
$jsonRpcRequest = array (
'jsonrpc' => '2.0',
'id' => $i++,
'method' => 'addPromotion',
'params' => array($sessionID, $promotionObject),
);
$result = callRPC((object)$jsonRpcRequest, $host);
var_dump($result);