Scriptable Cart Gift Card Example for SuiteCommerce or SCA
This example shows how to create a script to support a gift card special offer. Specifically, if a shopper buys a gift card worth $50 or more, that order qualifies for free shipping.
To support this special offer, use a non-inventory item for sale with a price of $0 and two transaction column fields, one for the value of the gift card and another for the attached message.
The following sample script illustrates many of the recommendations described in Best Practices for Using Scriptable Cart with SuiteCommerce or SCA. The script shows how to develop a function for a donation item, and then return the custom functions at the end.
var giftcard_item = new function giftcardItemSC ()
{
var self = this;
self.needsCommit = false;
self.isProcessing = false;
self.updateShipping = false;
/* Returns a Object structure with specific customer item pricing. */
this.setCustomizationPricing = function ()
{
var giftcardRate = nlapiGetCurrentLineItemValue('item', 'custcol_giftcard_rate')
, currentRate = nlapiGetCurrentLineItemValue('item', 'rate');
if (giftcardRate && currentRate && (currentRate !== giftcardRate))
{
self.updateShipping = true;
nlapiSetCurrentLineItemValue('item', 'price', '-1', true, true);
nlapiSetCurrentLineItemValue('item', 'rate', '' + giftcardRate, true, true);
self.needsCommit = true;
}
};
this.loopAndProcessCartLines = function (type, action)
{
if (type === 'item' && action === 'commit')
{
var lines = nlapiGetLineItemCount('item');
for (var i = 1; i <= lines; i++)
{
nlapiSelectLineItem('item', i);
self.setCustomizationPricing();
if (self.needsCommit)
{
nlapiCommitLineItem('item');
self.needsCommit = false;
}
}
}
};
this.hasGiftCardItem = function ()
{
var lines = nlapiGetLineItemCount('item')
, i = 1
, has = false
, giftcardRate;
while (i <= lines && !has)
{
giftcardRate = nlapiGetLineItemValue('item', 'custcol_giftcard_rate', i);
has = giftcardRate !== '' && parseFloat(giftcardRate) !== 0 && parseFloat(giftcardRate) >= 50;
i++;
}
return has;
};
return {
recalc: function (type, action)
{
if (nlapiGetContext().getExecutionContext() !== 'webstore') return true
if (self.isProcessing) return
self.isProcessing = true;
self.loopAndProcessCartLines(type, action);
self.isProcessing = false;
return true
}
, validateLine: function (type)
{
if (nlapiGetContext().getExecutionContext() !== 'webstore') return true
if (type !== 'item') return true
var giftcardRate = nlapiGetCurrentLineItemValue('item', 'custcol_giftcard_rate');
if ((nlapiGetCurrentLineItemValue('item', 'item') === '4500') && (giftcardRate === '' || parseFloat(giftcardRate) === 0))
{
alert('Please enter a GiftCard Amount.');
return false
}
return true
}
, fieldChange: function (type, name, linenum)
{
if (nlapiGetContext().getExecutionContext() !== 'webstore') return true
if (name === 'shipmethod' || name === 'rate')
{
if (self.isProcessing && !self.updateShipping) return true;
if (self.hasGiftCardItem())
{
self.isProcessing = true;
self.updateShipping = false;
nlapiSetFieldValue('shippingcost', 0, true, true);
self.isProcessing = false;
}
}
}
, postSourcing: function (type, name)
{
if (nlapiGetContext().getExecutionContext() !== 'webstore') return true
if (name === 'shippingtaxcode')
{
if (self.hasGiftCardItem())
{
if (self.isProcessing) return true
self.isProcessing = true;
nlapiSetFieldValue('shippingtax1rate', 0, true, true);
self.isProcessing = false;
}
}
}
};
}();
Note that in this example, you create a variable called gitcard_item
, so when you update the record for this script in NetSuite, you need to reference the method names using this name. For example, instead of simply entering recalc
in the record's Recalc Function field, you need to enter gitcard_item.recalc
, because these are being returned by the function.