Skip to content

Commit 251e445

Browse files
author
Chris Gårdenberg
committed
Added support for discount codes in bookings
1 parent d941e83 commit 251e445

10 files changed

Lines changed: 169 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
- Added `__bookSingleParticipant.php` and `__bookMultipleParticipants.php` to handle different settings.
77
- Fixing `frontend.js` to work with single participant-settings.
88

9+
## [0.9.12]
10+
### Added
11+
- Added option `eduadmin-allowDiscountCode`, to enable the customers to enter a discount code when they book participants for a event.
12+
- Bugfix: When copying contact to participant, correct field is now copied, instead of surname.
13+
- Added backend-api file to handle checking coupon codes
14+
- Added support to validate coupon codes against the api
15+
- Added support to recalculate the price and post the coupon with the Booking
16+
17+
918
## [0.9.11]
1019
### Added
1120
- Removed `margin-top: 1.0em; vertical-align: top;` from `.inputLabel` and replaced with `vertical-align: middle;`.
@@ -77,7 +86,8 @@
7786
- Added inquiry support in course
7887

7988

80-
[Unreleased]: https://github.com/MultinetInteractive/EduAdmin-WordPress/compare/v0.9.11...HEAD
89+
[Unreleased]: https://github.com/MultinetInteractive/EduAdmin-WordPress/compare/v0.9.12...HEAD
90+
[0.9.12]: https://github.com/MultinetInteractive/EduAdmin-WordPress/compare/v0.9.11...v0.9.12
8191
[0.9.11]: https://github.com/MultinetInteractive/EduAdmin-WordPress/compare/v0.9.10...v0.9.11
8292
[0.9.10]: https://github.com/MultinetInteractive/EduAdmin-WordPress/compare/v0.9.9.2.40...v0.9.10
8393
[0.9.9.2.40]: https://github.com/MultinetInteractive/EduAdmin-WordPress/compare/v0.9.9.2.39...v0.9.9.2.40

backend/modules/edu.api.coupon.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
date_default_timezone_set('UTC');
3+
4+
if(!function_exists('edu_api_check_coupon_code'))
5+
{
6+
function edu_api_check_coupon_code($request) {
7+
header("Content-type: application/json; charset=UTF-8");
8+
global $eduapi;
9+
10+
$edutoken = edu_decrypt("edu_js_token_crypto", getallheaders()["Edu-Auth-Token"]);
11+
12+
$objectID = $request['objectId'];
13+
$categoryID = $request['categoryId'];
14+
$code = $request['code'];
15+
$eduapi->debug = true;
16+
$vcode = $eduapi->CheckCouponCode($edutoken, $objectID, $categoryID, $code);
17+
$eduapi->debug = false;
18+
return json_encode($vcode);
19+
}
20+
}
21+
22+
if(isset($_REQUEST['module']) && $_REQUEST['module'] == "check_coupon_code")
23+
{
24+
echo edu_api_check_coupon_code($_REQUEST);
25+
}

content/script/educlient/edu.apiclient.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ edu.apiclient = {
209209
}
210210
});
211211
},
212+
CheckCouponCode: function(code, objectId, categoryId, onData) {
213+
jQuery.ajax({
214+
beforeSend: function(xhr) {
215+
xhr.setRequestHeader('Edu-Auth-Token', edu.apiclient.authToken);
216+
},
217+
url: edu.apiclient.baseUrl + '?module=check_coupon_code',
218+
type: 'POST',
219+
data: {
220+
code: code,
221+
objectId: objectId,
222+
categoryId: categoryId
223+
},
224+
success: function(d) {
225+
if(onData && typeof onData == 'function') {
226+
onData(d);
227+
}
228+
}
229+
});
230+
},
212231
GetCookie: function (name) {
213232
try {
214233
var cookie = document.cookie;

content/script/frontendjs.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var eduBookingView = {
55
SingleParticipant: false,
66
MaxParticipants: 0,
77
CurrentParticipants: 0,
8+
DiscountPercent: 0,
89
AddParticipant: function() {
910
if(!eduBookingView.SingleParticipant) {
1011
if(eduBookingView.MaxParticipants == -1 || eduBookingView.CurrentParticipants < eduBookingView.MaxParticipants)
@@ -171,8 +172,8 @@ var eduBookingView = {
171172
}
172173
}
173174

174-
if(totalPriceDiscountPercent != 0) {
175-
var disc = (totalPriceDiscountPercent / 100) * newPrice;
175+
if(totalPriceDiscountPercent != 0 || eduBookingView.DiscountPercent != 0) {
176+
var disc = ((totalPriceDiscountPercent + eduBookingView.DiscountPercent) / 100) * newPrice;
176177
newPrice = newPrice - disc;
177178
}
178179

@@ -215,7 +216,7 @@ var eduBookingView = {
215216
var tCivReg = document.querySelector('.contactCivReg')
216217
if(tCivReg) {
217218
var cCivReg = document.getElementById('edu-contactCivReg').value;
218-
tCivReg.value = cLastName;
219+
tCivReg.value = cCivReg;
219220
}
220221

221222
if(contact == 1 && !this.AddedContactPerson) {
@@ -235,6 +236,30 @@ var eduBookingView = {
235236
}
236237
},
237238
AddedContactPerson: false,
239+
ValidateDiscountCode: function() {
240+
edu.apiclient.CheckCouponCode(
241+
jQuery('#edu-discountCode').val(),
242+
jQuery('.validateDiscount').data('objectid'),
243+
jQuery('.validateDiscount').data('categoryid'),
244+
function(data) {
245+
if(data) {
246+
jQuery('#edu-discountCodeID').val(data.CouponID);
247+
eduBookingView.DiscountPercent = data.DiscountPercent;
248+
eduBookingView.UpdatePrice();
249+
} else {
250+
// Invalid code
251+
var codeWarning = document.getElementById('edu-warning-discount');
252+
if(codeWarning) {
253+
codeWarning.style.display = 'block';
254+
setTimeout(function() {
255+
var codeWarning = document.getElementById('edu-warning-discount');
256+
codeWarning.style.display = '';
257+
}, 5000);
258+
}
259+
}
260+
}
261+
);
262+
},
238263
CheckValidation: function() {
239264
var terms = document.getElementById('confirmTerms');
240265
if(terms) {

content/template/bookingTemplate/__bookMultipleParticipants.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@
369369
$bi->OccasionPriceNameLnkID = $_POST['edu-pricename'];
370370
}
371371

372+
if(isset($_POST['edu-discountCodeID']) && $_POST['edu-discountCodeID'] != "0") {
373+
$bi->CouponID = $_POST['edu-discountCodeID'];
374+
}
375+
372376
$bi->CustomerReference = (!empty($_POST['invoiceReference']) ? trim($_POST['invoiceReference']) : trim(str_replace(';', ' ', $contact->ContactName)));
373377
$eventCustomerLnkID = $eduapi->CreateSubEventBooking(
374378
$edutoken,

content/template/bookingTemplate/template_A.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,29 @@
289289
}
290290
?>
291291
<?php include_once("questionView.php"); ?>
292+
<?php
293+
if(get_option('eduadmin-allowDiscountCode', false)) {
294+
?>
295+
<label>
296+
<div class="inputLabel">
297+
<?php edu_e("Discount code"); ?>
298+
</div>
299+
<div class="inputHolder">
300+
<input type="text" name="edu-discountCode" id="edu-discountCode" style="width: 78%;" placeholder="<?php edu_e("Discount code"); ?>" />
301+
<button class="validateDiscount"
302+
style="width: 20%;"
303+
data-categoryid="<?php echo @esc_attr($selectedCourse->CategoryID); ?>"
304+
data-objectid="<?php echo @esc_attr($selectedCourse->ObjectID); ?>"
305+
onclick="eduBookingView.ValidateDiscountCode(); return false;">
306+
<?php edu_e("Validate"); ?>
307+
</button>
308+
<input type="hidden" name="edu-discountCodeID" id="edu-discountCodeID" />
309+
</div>
310+
</label>
311+
<div class="edu-modal warning" id="edu-warning-discount">
312+
<?php edu_e("Invalid discount code, please check your code and try again."); ?>
313+
</div>
314+
<?php } ?>
292315
<div class="sumTotal"><?php edu_e('Total sum:'); ?> <span id="sumValue" class="sumValue"></span></div>
293316

294317
<?php

eduadmin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Plugin URI: http://www.eduadmin.se
1313
* Description: EduAdmin plugin to allow visitors to book courses at your website
1414
* Tags: booking, participants, courses, events, eduadmin, lega online
15-
* Version: 0.9.11
15+
* Version: 0.9.12
1616
* Requires at least: 3.0
1717
* Tested up to: 4.7
1818
* Author: Chris Gårdenberg, MultiNet Interactive AB
@@ -79,7 +79,7 @@ function edu_call_home()
7979
'wpVersion' => $wp_version,
8080
'token' => get_option('eduadmin-api-key'),
8181
'officialVersion' => file_exists(dirname(__FILE__) . "/.official.plugin.php"),
82-
'pluginVersion' => '0.9.11'
82+
'pluginVersion' => '0.9.12'
8383
);
8484

8585
$callHomeUrl = 'http://ws10.multinet.se/edu-plugin/wp_phone_home.php';

includes/_options.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function eduadmin_settings_init()
4040
register_setting('eduadmin-booking', 'eduadmin-allowInterestRegObject');
4141
register_setting('eduadmin-booking', 'eduadmin-allowInterestRegEvent');
4242
register_setting('eduadmin-booking', 'eduadmin-hideSubEventDateTime');
43+
register_setting('eduadmin-booking', 'eduadmin-allowDiscountCode');
4344

4445
/* Phrase settings */
4546
register_setting('eduadmin-phrases', 'eduadmin-phrases');

includes/bookingSettings.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ function edu_writeOptions($g, $array, $depth, $selectedOption)
9999
<?php _e("Participant is also customer and contact (Only allow a single participant)", "eduadmin"); ?>
100100
</label>
101101
<br />-->
102+
<?php
103+
$allowDiscountCode = get_option('eduadmin-allowDiscountCode', false);
104+
?>
105+
<label>
106+
<input type="checkbox" name="eduadmin-allowDiscountCode"<?php echo ($allowDiscountCode === "true" ? " checked=\"checked\"" : ""); ?> value="true" />
107+
<?php _e("Allow end customers to use discount codes", "eduadmin"); ?>
108+
</label>
102109
<h4><?php _e("Field order", "eduadmin"); ?></h4>
103110
<?php
104111
$fieldOrder = get_option('eduadmin-fieldOrder', 'contact_customer');

includes/loApiClient.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class EduAdminClient
55
{
66
protected $__server;
77
public $debug = false;
8-
public $timers;
8+
protected $timers;
99

1010
public function __construct()
1111
{
@@ -14,7 +14,7 @@ public function __construct()
1414
$this->__server = new SoapClient(
1515
ServiceUrl,
1616
array(
17-
'trace' => 1,
17+
'trace' => 0,
1818
'cache_wsdl' => WSDL_CACHE_BOTH
1919
)
2020
);
@@ -458,6 +458,14 @@ public function GetCompanyAttributeXml($authToken, $sort, $filter) {
458458
return $this->__callServer($param, 'GetCompanyAttributeXml');
459459
}
460460

461+
public function GetCompanyLogoUrl($authToken) {
462+
$param = array(
463+
'authToken' => $authToken
464+
);
465+
466+
return $this->__callServer($param, 'GetCompanyLogoUrl');
467+
}
468+
461469
public function GetConfirmationEmailMessage($authToken, $eclID, $documentID) {
462470
$param = array(
463471
'authToken' => $authToken,
@@ -1231,6 +1239,16 @@ public function GetEventXml($authToken, $sort, $filter) {
12311239
return $this->__callServer($param, 'GetEventXml');
12321240
}
12331241

1242+
public function GetGetUserLocationXml($authToken, $sort, $filter) {
1243+
$param = array(
1244+
'authToken' => $authToken,
1245+
'sort' => $sort,
1246+
'filter' => $filter
1247+
);
1248+
1249+
return $this->__callServer($param, 'GetGetUserLocationXml');
1250+
}
1251+
12341252
public function GetGrade($authToken, $sort, $filter) {
12351253
$param = array(
12361254
'authToken' => $authToken,
@@ -1753,6 +1771,16 @@ public function GetUserAttributeXml($authToken, $sort, $filter) {
17531771
return $this->__callServer($param, 'GetUserAttributeXml');
17541772
}
17551773

1774+
public function GetUserLocation($authToken, $sort, $filter) {
1775+
$param = array(
1776+
'authToken' => $authToken,
1777+
'sort' => $sort,
1778+
'filter' => $filter
1779+
);
1780+
1781+
return $this->__getArray('UserLocation', $this->__callServer($param, 'GetUserLocation'))->UserLocation;
1782+
}
1783+
17561784
public function GetValidCoupons($authToken, $objectID, $categoryID) {
17571785
$param = array(
17581786
'authToken' => $authToken,
@@ -2396,7 +2424,9 @@ private function __callServer($params, $methodName)
23962424
if($this->debug)
23972425
$this->__debug();
23982426
$this->timers[$methodName . '__callServer'] = microtime(true) - $this->timers[$methodName . '__callServer'];
2399-
return $result->{$methodName/*$d*/ . 'Result'};
2427+
if(isset($result->{$methodName/*$d*/ . 'Result'}))
2428+
return $result->{$methodName/*$d*/ . 'Result'};
2429+
return null;
24002430
}
24012431

24022432
private function __debug($result = null)
@@ -2707,6 +2737,8 @@ class CertificatePersonV2 {
27072737
var $CertificateFromEventIDs;
27082738
var $CertificateFromPersonCertificateIDs;
27092739
var $Subjects;
2740+
var $CertificateAfterRetest;
2741+
var $CertificatePersonComment;
27102742
var $PersonID;
27112743
var $PersonFirstName;
27122744
var $PersonLastName;
@@ -2727,6 +2759,7 @@ function __construct() {
27272759
$this->CertificateFromEventIDs = array();
27282760
$this->CertificateFromPersonCertificateIDs = array();
27292761
$this->Subjects = array();
2762+
$this->CertificateAfterRetest = null;
27302763
$this->PersonID = 0;
27312764
$this->CustomerID = 0;
27322765
$this->CertificateID = 0;
@@ -2803,6 +2836,7 @@ function __construct() {
28032836
class Coupon {
28042837
var $CouponID;
28052838
var $Code;
2839+
28062840
var $DiscountPercent;
28072841
var $CouponDescription;
28082842
var $ValidFrom;
@@ -2853,7 +2887,6 @@ function __construct() {
28532887
}
28542888
}
28552889

2856-
28572890
class CustomerAttribute {
28582891
var $CustomerID;
28592892
var $CustomerAttributeID;
@@ -3077,7 +3110,7 @@ function __construct() {
30773110
$this->CanLogin = null;
30783111
$this->DiscountPercent = null;
30793112
$this->ParticipantDiscountPercent = null;
3080-
$this->NotCreditworthy = false;
3113+
$this->NotCreditworthy = null;
30813114
$this->CustomerID = 0;
30823115
$this->CustomerGroupID = null;
30833116
$this->VatFree = null;
@@ -4465,6 +4498,17 @@ function __construct() {
44654498
}
44664499
}
44674500

4501+
class UserLocation {
4502+
var $UserID;
4503+
var $LocationID;
4504+
4505+
function __construct() {
4506+
$this->UserID = 0;
4507+
$this->LocationID = 0;
4508+
}
4509+
}
4510+
4511+
44684512
class XFilter
44694513
{
44704514
var $FilterName = '';

0 commit comments

Comments
 (0)