Documentation for the entire Pricing Script engine. Type declarations can be found in the NPM package and can be used for your TypeScript and JavaScript projects to get full IntelliSense support.
The item management system provides access to all product-related data and functionality needed for pricing calculations. This includes the main product item, its attributes, pricing tiers, and versioning information.
The file handling system provides comprehensive information about uploaded files, allowing scripts to make pricing decisions based on file characteristics.
The system configuration provides access to script settings, parameters, and output functions that control behavior and functionality.
Configuration.ScriptConfig(), so one script can be reused with different settings.Constants.MessageArea chooses where the message is shown, such as only in the basket.parseCsv parses CSV text, and setTierPrice fills the product page tier price table.Helper methods provide utility functions for common pricing calculations and data manipulation tasks, making it easier to write complex pricing logic.
getCheckoutAttributeValue / setCheckoutAttributeValue. Available in product and checkout attribute scripts whenever there is a customer. The admin product Test button runs without a customer, so Session is undefined there; guard with if (typeof Session !== "undefined").Scripts run on an ES5 JavaScript engine. Write plain ES5 (var, function, indexOf); let, const, arrow functions, template literals, includes and startsWith fail at runtime. To get IntelliSense and type checking in your editor, install the package and use this tsconfig.json; the .js files can then be pasted into the admin script editor unchanged:
{
"compilerOptions": {
"target": "ES5",
"lib": ["ES5"],
"allowJs": true,
"checkJs": true,
"noEmit": true,
"strict": true,
"types": ["@infigo-official/types-for-pricing-script"]
},
"include": ["src/**/*"]
}
Scripts return the price from the top level, which TypeScript reports as an error. Put // @ts-ignore on the line above that return.
Strings, numbers and booleans that come from the platform (Item.ScriptContext, Item.Price, attribute values, CSV cells, role names) are wrapped objects in the script engine, so strict comparisons never match them:
| Instead of | Use |
|---|---|
Item.ScriptContext === "Product" |
Item.ScriptContext == "Product" |
Item.CustomerRoles.indexOf("VIP") >= 0 |
HelperMethods.Contains(Item.CustomerRoles, "VIP") |
rows[0].indexOf("Price") |
a loop comparing each rows[0][c] == "Price" |
String methods such as indexOf and toLowerCase work on platform strings. To get a plain JavaScript value, convert it with String(...) or Number(...).
When working with pricing script objects, data is read-only and accessed through the provided interfaces; the exceptions are Item.setAttributeValue, Session.setCheckoutAttributeValue and setTierPrice. The script receives the current state of the pricing context and returns the calculated price or pricing adjustments.
Updates to the pricing system are handled through the return value of the script, which can include price modifications, attribute adjustments, and other pricing-related changes.
Pricing scripts are triggered by various events in the pricing system:
The script can react to these events by recalculating prices, adjusting attributes, or providing user feedback.
Pricing scripts can store and retrieve data in various ways:
The pricing script system supports various file types for file-based pricing calculations:
For document-based pricing, the system provides access to:
Simple pricing calculations can be performed using basic arithmetic operations on the Item.Price property and attribute adjustments.
Complex pricing models can be implemented using the tier system, which supports:
Dynamic pricing can be calculated based on selected attributes:
File characteristics can influence pricing decisions:
Pricing scripts should validate their inputs and handle edge cases:
Robust pricing scripts should include error recovery mechanisms:
Simple pricing calculation based on quantity and base price:
// Basic price calculation with markup
var basePrice = Item.Price;
var quantity = Item.Quantity;
var markup = 1.25; // 25% markup
var totalPrice = basePrice * quantity * markup;
debug("Base: $" + basePrice + " x " + quantity + " = $" + totalPrice);
return totalPrice;
Fixed price with quantity discounts:
var unitPrice = 10.00;
var quantity = Item.Quantity;
var discount = 0;
// Apply quantity-based discounts
if (quantity >= 100) {
discount = 0.20; // 20% off for 100+
} else if (quantity >= 50) {
discount = 0.15; // 15% off for 50+
} else if (quantity >= 25) {
discount = 0.10; // 10% off for 25+
}
var finalPrice = unitPrice * quantity * (1 - discount);
debug("Quantity: " + quantity + ", Discount: " + (discount * 100) + "%");
return finalPrice;
Using built-in tier pricing with HelperMethods:
// Define pricing tiers
var tiers = [
{ Quantity: 1, Price: 5.00, CustomerRole: "" },
{ Quantity: 50, Price: 4.00, CustomerRole: "" },
{ Quantity: 100, Price: 3.50, CustomerRole: "" },
{ Quantity: 250, Price: 3.00, CustomerRole: "" },
{ Quantity: 500, Price: 2.50, CustomerRole: "" },
{ Quantity: 1000, Price: 2.00, CustomerRole: "" }
];
// Find the appropriate tier for the current quantity
var tier = HelperMethods.FindTier(Item.Quantity, tiers);
if (tier) {
var totalPrice = tier.Price * Item.Quantity;
debug("Tier price: $" + tier.Price + " for qty " + Item.Quantity);
return totalPrice;
}
return Item.Price * Item.Quantity;
Interpolated pricing for smooth transitions:
var tiers = [
{ Quantity: 1, Price: 10.00, CustomerRole: "" },
{ Quantity: 100, Price: 8.00, CustomerRole: "" },
{ Quantity: 500, Price: 6.00, CustomerRole: "" },
{ Quantity: 1000, Price: 4.00, CustomerRole: "" }
];
// Get interpolated price between tiers
var unitPrice = HelperMethods.InterpolatePrice(Item.Quantity, tiers);
var totalPrice = unitPrice * Item.Quantity;
debug("Interpolated unit price: $" + unitPrice.toFixed(2));
return totalPrice;
Apply different pricing based on customer roles:
var basePrice = Item.Price * Item.Quantity;
var discount = 0;
// Check for VIP customers
if (HelperMethods.Contains(Item.CustomerRoles, "VIP")) {
discount = 0.25; // 25% VIP discount
debug("VIP customer - 25% discount applied");
}
// Check for wholesale customers
else if (HelperMethods.Contains(Item.CustomerRoles, "Wholesale")) {
discount = 0.20; // 20% wholesale discount
debug("Wholesale customer - 20% discount applied");
}
// Check for registered customers
else if (HelperMethods.Contains(Item.CustomerRoles, "Registered")) {
discount = 0.05; // 5% member discount
debug("Registered customer - 5% discount applied");
}
return basePrice * (1 - discount);
Role-based tier pricing:
// Define tiers with customer role support
var tiers = [
{ Quantity: 1, Price: 10.00, CustomerRole: "" },
{ Quantity: 50, Price: 8.00, CustomerRole: "Registered" },
{ Quantity: 50, Price: 7.00, CustomerRole: "Wholesale" },
{ Quantity: 50, Price: 6.00, CustomerRole: "VIP" },
{ Quantity: 100, Price: 6.00, CustomerRole: "" },
{ Quantity: 100, Price: 5.00, CustomerRole: "Wholesale" },
{ Quantity: 100, Price: 4.00, CustomerRole: "VIP" }
];
// FindTier automatically considers customer roles
var tier = HelperMethods.FindTier(Item.Quantity, tiers, Item.CustomerRoles);
if (tier) {
return tier.Price * Item.Quantity;
}
return Item.Price * Item.Quantity;
Parse and use CSV data for pricing:
// CSV pricing data (could come from Configuration.ScriptConfig)
var csvData = "Quantity,Price,Setup\n1,5.00,25.00\n50,4.50,20.00\n100,4.00,15.00\n500,3.50,10.00";
// Parse CSV into array
var rows = HelperMethods.CSV.parse(csvData);
// Skip header row, find matching tier
var quantity = Item.Quantity;
var unitPrice = 5.00;
var setupFee = 25.00;
for (var i = 1; i < rows.length; i++) {
var row = rows[i];
if (quantity >= row[0]) {
unitPrice = row[1];
setupFee = row[2];
}
}
var totalPrice = (unitPrice * quantity) + setupFee;
debug("Unit: $" + unitPrice + ", Setup: $" + setupFee + ", Total: $" + totalPrice);
return totalPrice;
Look up a price row in a large CSV file from Global Data by attribute values:
// prices.csv has the columns Material, Size, Price
var rows = Item.getRowsFromLookup("prices.csv", ["Material", "Size"],
[Item.getAttributeValue("Material"), Item.getAttributeValue("Size")]);
// rows[0] is the header row; matches start at rows[1]
if (!rows || rows.length < 2) {
error("No price found for this combination", Constants.MessageArea.ProductDetails);
return 0;
}
// Values are platform strings, so find the column with == rather than indexOf
for (var c = 0; c < rows[0].length; c++) {
if (rows[0][c] == "Price") {
return parseFloat(rows[1][c]) * Item.Quantity;
}
}
return 0;
Calculate pricing based on uploaded file properties:
var basePrice = 0;
// Uploaded files belong to the product's file upload attributes
for (var i = 0; i < Item.Attributes.length; i++) {
var attr = Item.Attributes[i];
if (attr.Type != "FileUpload") {
continue;
}
var file = Item.getFileInfo(String(attr.Id), false);
if (!file) {
continue; // nothing uploaded for this attribute
}
// Price per page for PDFs
if (file.MimeType == "application/pdf") {
var pageCount = file.NumberOfPages > 0 ? file.NumberOfPages : 1;
var pricePerPage = 0.50;
basePrice += pageCount * pricePerPage;
debug("PDF: " + pageCount + " pages @ $" + pricePerPage + "/page");
}
// Price by file size for images
else if (file.MimeType.indexOf("image/") == 0) {
var sizeInMB = file.Size / (1024 * 1024);
var pricePerMB = 2.00;
basePrice += sizeInMB * pricePerMB;
debug("Image: " + sizeInMB.toFixed(2) + "MB @ $" + pricePerMB + "/MB");
}
}
return basePrice * Item.Quantity;
Apply discounts based on total cart value:
// Calculate total cart value from session
var cartTotal = 0;
for (var i = 0; i < Session.CartItems.length; i++) {
var cartItem = Session.CartItems[i];
cartTotal += cartItem.Price * cartItem.Quantity;
}
// Apply cart-wide discount tiers
var cartDiscount = 0;
if (cartTotal >= 500) {
cartDiscount = 0.15; // 15% off orders $500+
} else if (cartTotal >= 250) {
cartDiscount = 0.10; // 10% off orders $250+
} else if (cartTotal >= 100) {
cartDiscount = 0.05; // 5% off orders $100+
}
var itemPrice = Item.Price * Item.Quantity;
var discountedPrice = itemPrice * (1 - cartDiscount);
debug("Cart total: $" + cartTotal + ", Discount: " + (cartDiscount * 100) + "%");
return discountedPrice;
Calculate pricing based on dimensions:
// Get dimensions from item
var width = Item.Width || 0;
var height = Item.Height || 0;
// Calculate area in square inches
var areaInSqIn = width * height;
// Convert to square feet
var areaInSqFt = areaInSqIn / 144;
// Price per square foot with minimum
var pricePerSqFt = 2.50;
var minimumPrice = 10.00;
var calculatedPrice = areaInSqFt * pricePerSqFt * Item.Quantity;
var finalPrice = Math.max(calculatedPrice, minimumPrice);
debug("Area: " + areaInSqFt.toFixed(2) + " sq ft @ $" + pricePerSqFt + "/sq ft");
return finalPrice;
Apply pricing based on customer department (Session-level):
var basePrice = Item.Price * Item.Quantity;
var departmentDiscount = 0;
// Check customer department
switch (Session.Department) {
case "Marketing":
departmentDiscount = 0.15;
debug("Marketing department - 15% discount");
break;
case "Sales":
departmentDiscount = 0.10;
debug("Sales department - 10% discount");
break;
case "Education":
departmentDiscount = 0.20;
debug("Education department - 20% discount");
break;
default:
debug("Standard pricing applied");
}
return basePrice * (1 - departmentDiscount);
Calculate price adjustments from selected attributes:
var basePrice = Item.Price;
var attributeAdjustment = 0;
// Get attribute adjustments with tier support
attributeAdjustment = HelperMethods.GetAttributePriceAdjustment(
Item.Quantity,
Item.CustomerRoles
);
// Calculate total
var totalPrice = (basePrice + attributeAdjustment) * Item.Quantity;
debug("Base: $" + basePrice + ", Attributes: $" + attributeAdjustment);
return totalPrice;
Manual attribute iteration:
var basePrice = Item.Price;
var adjustments = 0;
// Item.Attributes holds one entry per selected value
for (var i = 0; i < Item.Attributes.length; i++) {
var attr = Item.Attributes[i];
// Text and file attributes have no options (empty Values) and no price adjustment
if (attr.Values.length > 0) {
var adjustment = attr.PriceAdjustmentIsPercentage
? basePrice * attr.PriceAdjustment / 100
: attr.PriceAdjustment;
adjustments += adjustment;
debug(attr.Key + ": " + attr.Value + " (+$" + adjustment + ")");
}
}
return (basePrice + adjustments) * Item.Quantity;
Apply pricing based on checkout/shipping information:
var basePrice = Item.Price * Item.Quantity;
var shippingSurcharge = 0;
// Check shipping destination country
if (CheckoutItem.Address && CheckoutItem.Address.Country) {
var countryCode = CheckoutItem.Address.Country.TwoLetterIsoCode;
// International shipping surcharge
if (countryCode != "US") {
shippingSurcharge = 15.00;
debug("International shipping to " + countryCode + " - $15 surcharge");
}
// Remote area surcharge
if (countryCode == "AK" || countryCode == "HI") {
shippingSurcharge = 10.00;
debug("Alaska/Hawaii shipping - $10 surcharge");
}
}
// Business address discount
if (CheckoutItem.Address && CheckoutItem.Address.Company) {
basePrice *= 0.95; // 5% B2B discount
debug("Business address - 5% B2B discount applied");
}
return basePrice + shippingSurcharge;
Generated using TypeDoc