Use SuiteScript to accept charitable contributions on your web site.
Create an item record for display on the web store as a charitable donation item. The shopper adds this item to the shopping cart and enters the amount of donation he or she wants to make. After the item is added to the cart, the total amount of the order is recalculated to include the non taxable donation.
Use the steps below to create the charitable donation item record, and custom item options required to run the sample script. Note that you must complete the following setup steps to use the code sample successfully.
// BEGIN CUSTOM FUNCTIONS FOR CHARITABLE CONTRIBUTIONS
function customValidateLine(type)
{
// All validations occur in the item tab.
if (type != 'item')
{
return true;
}
// Note that validation routines are called differently from other events, because the return
// value of each function must be tested before going on to the next function. If any function
// returns false, stop processing and return false.
if (!charity())
{
return false;
}
// All validations passed. Return true.
return true;
}
//END CUSTOM FUNCTIONS FOR CHARITY CONTRIBUTION
///CHARITY CONTRIBUTION
function charity()
{
// Internal ID of the charity item record.
var charityId = 384;
// Ignore any items other than the charity item record.
if (charityId != nlapiGetCurrentLineItemValue('item', 'item'))
{
return true;
}
debug('Charity item detected');
// Validate that the amount in the custom field is greater than zero.
var contribAmount = nlapiGetCurrentLineItemValue('item','custcol_contribution');
debug('Contribution '+contribAmount+' read in');
if (isEmpty(contribAmount) || contribAmount <= 0)
{
alert('You must contribute an amount that is greater than zero!');
return false;
}
debug('Contribution valid - setting the item rate');
// Set the price of the line item to the contribution amount.
setCurrentLineRate(contribAmount);
return true;
}
///END CHARITY CONTRIBUTION
//BEGIN UTILITY FUNCTIONS
function isEmpty(val)
{
return (val == null || val == '');
}
function isNotEmpty(val)
{
return !isEmpty(val);
}
// This function updates the rate and recalculates the value for amount.
function setCurrentLineRate(rate)
{
nlapiSetCurrentLineItemValue('item', 'rate', rate, true, true);
var qty = parseInt(nlapiGetCurrentLineItemValue('item', 'quantity'));
var amount = qty * rate;
nlapiSetCurrentLineItemValue('item', 'amount', amount, true, true);
}
//END UTILITY FUNCTIONS
Deploy Your Script to the Shopping Cart
NetSuite recommends that you test your script thoroughly before running it in the shopping cart. You must follow a series of steps to load the script into your NetSuite account, and then run the script for testing purposes.