The JSMA voucher allows access and manipulation of vouchers (voucher campaigns) and tokens (voucher codes) used in carts and orders.
What can JSMA voucher do for me?With the JSMA voucher you can create, edit and delete vouchers and tokens for carts and orders on your shopping website. It allows you to dynamically generate vouchers and import existing tokens - which enables a synchronization between IKONA and external voucher systems. |
Here is an example on how to write data to a JSMA voucher:
function writeVoucher(preparedTokens) { [...] var imported = voucher.importTokens(preparedTokens); imported.update({'value' : 10, 'validFrom' : date.getTime(), 'validUntil' : dateInOneWeek.getTime(), 'discountType' : 'relative'}); imported.set('products.whitelist', portal.get('voucherEasterWhitelist')); [...] } |
Here is an example on how to read data from a JSMA voucher:
function getCartItems(cart, parameters) { var items = cart.items; var resultItems = []; for (var i=0;i<items.length;i++) { var item = items[i]; var resultItem = {}; [...] if (item.type === "voucher") { var voucher = item.voucher; resultItem.voucher = { id : voucher.id, uuid : voucher.uuid, value : voucher.value, display : voucher.display, isValid : voucher.isValid, type : voucher.type, withShipping : voucher.withShipping, validFrom : voucher.validFrom, validUntil : voucher.validUntil, }; } resultItems.push(resultItem); } return resultItems; } |
The JSMA voucher provides the following methods:
Activates the voucher. Returns true if the operation was successfull.
... voucher.activate() // => true ... |
Adds a token to a voucher. Returns true if the operation was successfull.
... voucher.addToken(token) // => true ... |
Creates an array of voucher tokens.
Example
... voucher.createTokens(10); // => [{'token' : 'token1', 'used' : false, 'orderItemId' : null}, {'token' : 'token2', 'used' : false, 'orderItemId' : null}, ...] ... |
Parameter int count (required)
Positive integer. Determines how many tokens will be created.
Deactivates the voucher. Returns true if the operation was successfull.
... voucher.deactivate() ... |
Returns true if the voucher has been deleted successfully, otherwise false.
Example
... voucher.delete(); // => true ... |
Returns the display field of the voucher as a string.
... voucher.display // => '14% Valentinstag-Rabatt' ... |
Returns the flex attributes of a voucher
... voucher.flexAttributes // => {['products.whitelist' : 'rhino\.integration\.products\.EligibleProducts\.*']} ... |
Returns the value of key as string or null if no value is set under the key.
... voucher.get('procuts.whitelist') // => 'rhino\.integration\.products\.EligibleProducts\.*' ... |
The key to be looked up in the flexAttributes of the voucher.
Returns an array of tokens that belong to a voucher
Example
... voucher.getTokens(); // => [{'token' : 'xmas10', 'used' : true, 'orderItemId' : null},{'token' : 'xmas25', 'used' : false, 'orderItemId' : null}] ... |
Returns a boolean value depending on whether key is present for a given voucher.
... voucher.has('products.blacklist') // => false ... |
The key to be checked in the flexAttributes of the voucher.
Returns the ID field of a voucher as an integer
... voucher.id // => 3 ... |
Imports an existing array of tokens into a voucher
Example
... voucher.importTokens(['xmas10','xmas25']); ... |
Parameter [tokensArray] (required)
Array of voucher tokens
Increments the vale of a given key by one.
... voucher.increment('timesUsed') ... |
The key to be incremented in the flexAttributes of the voucher.
Returns true if a voucher is active and not expired, otherwise false.
... voucher.isValid // => true ... |
Removes the key from the flexAttributes of the voucher
... voucher.remove('products.blacklist') ... |
The key to be removed from the flexAttributes of the voucher.
Sets the value of a specified key for a given voucher
... voucher.set('promoteOnFrontPage', true, [options]) ... |
The key to be set in the flexAttributes of the order.
The value to be set for the key
Returns the discount type of the voucher as a string.
... voucher.type // => 'relative' ... |
Updates a voucher with the given attributes and returns it
Example
... voucher.update({'value' : 10, 'minValue' : 395, 'validFrom' : date.getTime(), 'validUntil' : dateInOneWeek.getTime(), 'discountType' : 'relative'}); ... |
The attributes object can take in the following keys:
int value
Value of the voucher. Must be a positive integer in cents for absolute vouchers or a percentage for relative vouchers.
int minValue (optional)
Minimal value a voucher can be redeemed for. Must be a positive integer in cents.
int validFrom
Time in milliseconds from when a voucher will be valid from.
int validUntil
Time in milliseconds up to when a voucher will be valid until.
Returns the UUID of the voucher as a string
... voucher.uuid // '9d4b97e5-ee02-42aa-b71d-49e07859f1b3' ... |
Returns the 'valid from' date of a voucher
... voucher.validFrom // => Mon Mar 26 2018 03:42:00 GMT ... |
Returns the 'valid until' date of a voucher
... voucher.validUntil // => Sun Apr 01 2018 03:42:00 GMT ... |
Returns the relative (percentage) or absolute (in cents) value of a voucher
... voucher.value // 495 ... |
Returns true if voucher affects shipping costs, otherwise false
... voucher.withShipping // => false ... |