eCommerce Javascript
This is the provided javascript options for further controlling/customising the ecommerce javascript provided by the CMS.
Below describes the various javascript options, such as overriding alert pop-ups, before and after JS events and triggers.
Override Alerts
Such customisation may be used in order to customise all ecommerce alerts (instead of overwriting window.alert
function that may be used elsewhere throughout the site)
The below script can be added to your existing JS file (or within a <script>
element on your site template)
<script>
window.EcommerceMessages = {
"AddedToCart" : "The item has been added to the shopping cart.",
"RequiredAttributesNotSelectedError" : "Please choose relevant options before adding to cart",
"NotEnoughInStockError" : "In stock is less then item quantity entered",
"ProductAttributeVariationIsDisabledError" : "This variation of the product is disabled.",
"InventoryProductNotFoundError" : "Product with this attribute is not in stock",
"InvalidMinimumUnitsError" : "Quantity entered is too small, please enter a larger quantity.",
"InvalidMaximumUnitsError" : "Quantity entered is too large, please enter a smaller quantity.",
"DestinationCountryMandatoryError" : "Please select a destination country",
"DiscountNotFoundError" : "Discount not found",
"DiscountMinOrderCostLimitationError" : "Shopping cart total cost is less than discount minimum order cost limit",
"DiscountInActiveError" : "Discount code is currently disabled",
"DiscountReleaseDateTimeError" : "Discount is not yet available",
"DiscountExpirationDateTimeError" : "Discount has expired",
"DiscountDifferentCurrencyError" : "Discount currency is different from domain currency",
"ShippingOptionNotSelectedError" : "Please select your shipping option",
"ShippingOptionDifferentCurrencyError" : "Shipping option has different currency than domain currency",
"ShippingOptionDifferentCountryError" : "Shipping option has different country than domain country",
"ShippingOptionEmptyProductError" : "Shopping cart has no product(s) for shipping",
"ShippingOptionMinWeightError" : "Specified weight of one or more products in the shopping cart is less than min weight allowed by selected shipping option",
"ShippingOptionMaxWeightError" : "Specified weight of one or more products in the shopping cart exceeds max weight allowed by selected shipping option",
"ShippingOptionMinWidthError" : "Specified width of one or more products in the shopping cart is less than min weight allowed by selected shipping option",
"ShippingOptionMaxWidthError" : "Specified width of one or more products in the shopping cart exceeds max weight allowed by selected shipping option",
"ShippingOptionMinHeightError" : "Specified height of one or more products in the shopping cart is less than min height allowed by selected shipping option",
"ShippingOptionMaxHeightError" : "Specified height of one or more products in the shopping cart exceeds max height allowed by selected shipping option",
"ShippingOptionMinDepthError" : "Specified depth of one or more products in the shopping cart is less than min depth allowed by selected shipping option",
"ShippingOptionMaxDepthError" : "Specified depth of one or more products in the shopping cart exceeds max depth allowed by selected shipping option",
"ShippingOptionMinOrderPriceError" : "Total order price excluding shipping is lower than minimum set amount. \r\nPlease add more items to shopping cart or choose different shipping option.",
"ShippingOptionMaxOrderPriceError" : "Shipping option maximum allowed price is higher than shopping cart order price",
"ShippingOptionDifferentCustomerTypeError" : "Shipping option customer type is different than site user type",
"GiftVoucherDifferentCurrencyError" : "Gift voucher currency is different from domain currency"
}
</script>
You may also need to include this within a document ready function to ensure the DOM is ready and the related scripts have loaded.
An example of doing this, when using jQuery, is below:
<script>
$(document).ready(function() {
// the above code here...
});
</script>
Other Examples
window.EcommerceMessages["AddedToCart"] = "";
Using the above script, the AddedToCart
alert will not be triggered at all (due to the empty message value).
window.EcommerceMessages["AddedToCart"] = function(messageAlias) {
//messageAlias == "AddedToCart"
alert("Added to cart custom mesage")
};
Using the above script, the custom function code will be executed instead of the alert.
messageAlias
is a property name of the object window.EcommerceMessages
.
Shop Events
"Before" Events
"Before" events can be used for additional custom Javascript logic and can interrupt the regular ecommerce Javascript event process (by use of the event.preventDefault();
function).
Example usage:
document.addEventListener("CMS_BeforeAddToCart", function (event) {
var data = event.data;
console.log("Event data: ", data);
//additional custom code here
});
All available "before" events and their returned data are listed below.
CMS_BeforeAddToCart
Fires just before an item is added to the cart (the AddToCart event).
Returned data:
{
"ProductId": int, (required)
"Quantity": int, (required)
"BuyNow": bool, (required)
"ProductAttributes": [
{
"AttributeId": "string",
"OptionsPointers": ["string"]
}
]
}
CMS_BeforeRemoveFromCart
Fires just before an item is removed from the cart (the RemoveFromCart event).
Returned data:
{
"CartLineId": "string",
"Quantity": 0,
"Target": removeItemButton
}
CMS_BeforeClearCart
Fires just before the cart is cleared (the ClearCart event).
Returned data:
{
"Target": clearButton
}
CMS_BeforeChangeAttributeOption
Fires just before one of an item's attribute options are changed (the ChangeAttributeOption event).
Returned data:
{
"ProductId": int,
"AttributeId": "string",
"Target": attributeInput
}
CMS_BeforeChangeGroupedProduct
Fires just before an item's grouped item is selected (the ChangeGroupedProduct event).
Returned data:
{
"SourceProductId": int,
"TargetProductId": int,
"LayoutName": "string",
"Target": gropedProductSelect
}
CMS_BeforeChangeProductQuantity
Fires just before an item's quantity in the cart is changed (the ChangeProductQuantity event).
Returned data:
{
"CartLineId": "string",
"Quantity": int,
"Target": itemQuantityField
}
CMS_BeforeChangeDestinationCountry
Fires just before the cart's destination country is changed (the ChangeDestinationCountry event).
Returned data:
{
"Value": "string",
"Target": countrySelect
}
CMS_BeforeChangeShippingOption
Fires just before the cart's shipping option is changed (the ChangeShippingOption event).
Returned data:
{
"Value": "string",
"Target": shippingOptionsSelect
}
CMS_BeforeChangeGiftVoucher
Fires just before the cart's gift voucher code is changed (the ChangeGiftVoucher event).
Returned data:
{
"Value": "string",
"Target": giftVoucherField
}
CMS_BeforeChangeDiscount
Fires just before the cart's discount code is changed (the ChangeDiscount event).
Returned data:
{
"Value": "string",
"Target": discountCodeField
}
CMS_BeforeChangeTaxCode
Fires just before the cart's tax code is changed ( the ChangeTaxCode event).
Returned data:
{
"Value": "string",
"Target": taxCodeSelect
}
CMS_BeforeGoToCheckout
Fires just before (the GoToCheckout event).
Returned data:
{
"Target": checkoutButton
}
"After" Events
"After" events can be used to retrieve data that was returned from the server after a ecommerce Javascript event in order to apply any custom Javascript logic (for example, a custom shopping cart widget).
Example usage:
document.addEventListener("CMS_AfterAddToCart", function (event) {
var data = event.data;
console.log("Event data: ", data);
//additional custom code here
});
All available "After" events and their returned data are listed below.
CMS_AfterAddToCart
Fires just after an item is added to the cart (the AddToCart event).
Returned data:
Add to Cart object (see below example)
CMS_AfterRemoveFromCart
Fires just after an item is removed from the cart (the RemoveFromCart event).
Returned data:
Shopping Cart object (see below example)
CMS_AfterClearCart
Fires just after the cart is cleared (the ClearCart event).
Returned data:
null
CMS_AfterChangeAttributeOption
Fires just after one of an item's attribute options are changed (the ChangeAttributeOption event).
Returned data:
{
"ProductId": int,
"AttributeId": "string",
"Target": attributeInput
}
CMS_AfterChangeGroupedProduct
Fires just after an item's grouped item is selected (the ChangeGroupedProduct event).
Returned data:
{
"SourceProductId": int,
"TargetProductId": int,
"LayoutHtml": "string",
"ProductHolder": productHolderDomElement
}
CMS_AfterChangeProductQuantity
Fires just after an item's quantity in the cart is changed (the ChangeProductQuantity event).
Returned data:
Shopping Cart object (see below example)
CMS_AfterChangeDestinationCountry
Fires just after the cart's destination country is changed (the ChangeDestinationCountry event).
Returned data:
Shopping Cart object (see below example)
CMS_AfterChangeShippingOption
Fires just after the cart's shipping option is changed (the ChangeShippingOption event).
Returned data:
Shopping Cart object (see below example)
CMS_AfterChangeGiftVoucher
Fires just after the cart's gift voucher code is changed (the ChangeGiftVoucher event).
Returned data:
Shopping Cart object (see below example)
CMS_AfterChangeDiscount
Fires just after the cart's discount code is changed (the ChangeDiscount event).
Returned data:
Shopping Cart object (see below example)
CMS_AfterChangeTaxCode
Fires just after the cart's tax code is changed ( the ChangeTaxCode event).
Returned data:
Shopping Cart object (see below example)
CMS_AfterGoToCheckout
Fires just after (the GoToCheckout event).
Returned data:
Shopping Cart object (see below example)
Add to Cart Object Sample
This is a sample of the type of data you'll receive for Add to Cart related events.
{
"RedirectUrl": null,
"InventoryData": {
"itemId": 2034,
"attributes": {
"12462596046050033672": {
"id": "12462596046050033672",
"isInventory": true,
"name": "Color",
"type": "Radiolist",
"required": true,
"optionIds": [
"16234894512508370985",
"16234894512508370986",
"16234894512508370987",
"16234894512508370988",
"16234894512508370989",
"16234894512508370990",
"16234894512508370991"
]
}
},
"variations": null,
"options": {
"16234894512508370985": {
"id": "16234894512508370985",
"attributeId": "12462596046050033672",
"name": "White",
"image": "/images/ecommerce/attr-white-color.png",
"price": 0.0000
},
"16234894512508370986": {
"id": "16234894512508370986",
"attributeId": "12462596046050033672",
"name": "Black",
"image": "/images/ecommerce/attr-black-color.png",
"price": 0.0000
},
"16234894512508370987": {
"id": "16234894512508370987",
"attributeId": "12462596046050033672",
"name": "Gray",
"image": "/images/ecommerce/attr-light-gray-color.png",
"price": 0.0000
},
"16234894512508370988": {
"id": "16234894512508370988",
"attributeId": "12462596046050033672",
"name": "Brown",
"image": "/images/ecommerce/attr-brown-color.png",
"price": 0.0000
},
"16234894512508370989": {
"id": "16234894512508370989",
"attributeId": "12462596046050033672",
"name": "Blue",
"image": "/images/ecommerce/attr-blue-color.png",
"price": 0.0000
},
"16234894512508370990": {
"id": "16234894512508370990",
"attributeId": "12462596046050033672",
"name": "Red",
"image": "/images/ecommerce/attr-red-color.png",
"price": 0.0000
},
"16234894512508370991": {
"id": "16234894512508370991",
"attributeId": "12462596046050033672",
"name": "Yellow",
"image": "/images/ecommerce/attr-yellow-color.png",
"price": 0.0000
}
},
"inStockTotal": null,
"recommendedPrice": 0.0000,
"prices": {
"0": 40.0000
},
"taxInPercent": 2.0000,
"taxRate": 0.0200,
"enablePreOrder": true,
"maximumUnits": 0,
"minimumUnits": 0,
"QuantityThreshold": [{
"Quantity": 0,
"Price": 40.0000
}]
},
"ProductId": 2034
}
Shopping Cart Object Sample
This is a sample of the type of data you'll receive for Shopping Cart related events.
{
"ItemsCount": 22,
"HasGiftVoucherProducts": false,
"DiscountPrice": 0.0000,
"DiscountFormattedPrice": "$0.00",
"GiftVoucherPrice": 0.0000,
"GiftVoucherFormattedPrice": "$0.00",
"SubTotalPrice": 880.0000,
"SubTotalFormattedPrice": "$880.00",
"TaxPrice": 17.6000,
"TaxFormattedPrice": "$17.60",
"TotalPrice": 897.6000,
"TotalFormattedPrice": "$897.60",
"ShippingPrice": 0.0,
"ShippingFormattedPrice": "$0.00",
"ShippingTaxRate": 0.0200,
"ShippingTaxPercent": 2.0000,
"ShippingTaxPrice": 0.0,
"ShippingTaxFormattedPrice": "$0.00",
"ShippingTotalPrice": 0.0,
"ShippingTotalFormattedPrice": "$0.00",
"SubTotalTaxPrice": 17.6000,
"SubTotalTaxFormattedPrice": "$17.60",
"DiscountedSubTotalTaxPrice": 17.6000,
"DiscountedSubTotalTaxFormattedPrice": "$17.60",
"GrandTotalPrice": 897.6000,
"GrandTotalFormattedPrice": "$897.60",
"TotalPriceExcludingTaxAndGiftVoucherAmount": 880.0000,
"TotalFormattedPriceExcludingTaxAndGiftVoucherAmount": "$880.00",
"TotalPriceExcludingTax": 880.0000,
"TotalFormattedPriceExcludingTax": "$880.00",
"TaxRate": 0.0200,
"TaxPercent": 2.0000,
"ShippingTaxSettings": {
"Id": "3835385402823278593",
"CurrencyCountry": {
"Hash": 1677432418,
"CurrencyId": "12527484287403950218",
"CurrencyPointer": {
"Pointer": 12527484287403950218,
"TypeId": 2916782230,
"InstanceId": 138,
"DbTypeId": -1378185066,
"DbInstanceId": 138
},
"Currency": "USD",
"CountryId": "13700469718849159393",
"CountryPointer": {
"Pointer": 13700469718849159393,
"TypeId": 3189889183,
"InstanceId": 225,
"DbTypeId": -1105078113,
"DbInstanceId": 225
},
"CountryAbbriviation": "US"
},
"TaxSettings": {
"TaxAppliesTo": [
"GiftVouchers",
"ShippingOptions"
],
"AutoSelectTaxIfOnlyOneOptionalAvailable": true,
"HideIfOnlyOneTaxInTheList": false,
"EnableTaxDrop": true
},
"ShippingSettings": {
"ShippingCountriesId": [],
"DefaultCountryId": "13700469718849159393",
"IsMandatoryShippingCountry": false,
"SubtotalWithTax": true
}
},
"OrderLines": [{
"OrderLineId": "15049017874163171462",
"Quantity": 7,
"UnitPrice": 40.0000,
"TaxPercent": 2.0000,
"UnitFormattedPrice": "$40.00",
"UnitTaxPrice": 0.80000000,
"UnitTaxFormattedPrice": "$0.80",
"TaxRate": 0.0200,
"TaxPrice": 5.6000,
"TaxFormattedPrice": "$5.60",
"TotalPrice": 285.60,
"TotalFormattedPrice": "$285.60",
"TotalPriceWithoutTax": 280.0000,
"TotalFormattedPriceWithoutTax": "$280.00",
"Price": 40.0000,
"FormattedPrice": "$40.00",
"Type": "ModuleItem",
"ModuleId": 2034,
"UnitTotalPrice": 40.80,
"UnitTotalFormattedPrice": "$40.80",
"SKUCode": "65d1be86-2aed-46c0-a402-bd031f033a3e",
"Url": "/catalog/product-name-1-1-1-1-1",
"TaxCode": null,
"UnitRecommendedPrice": 0.0000,
"UnitRecommendedFormattedPrice": "$0.00",
"UnitRecommendedTaxPrice": 0.00000000,
"UnitRecommendedTaxFormattedPrice": "$0.00",
"UnitRecommendedTotalPrice": 0.00000000,
"UnitRecommendedTotalFormattedPrice": "$0.00",
"OnSale": false,
"Attributes": [{
"Id": "12462596046050033672",
"Name": "Color",
"AttributeType": 0,
"Price": 0.0000,
"FormattedPrice": "$0.00",
"TaxPrice": 0.00000000,
"TaxFormattedPrice": "$0.00",
"TotalPrice": 0.00000000,
"TotalFormattedPrice": "$0.00",
"Options": [{
"Id": "16234894512508370988",
"Name": "Brown",
"TaxPrice": 0.00000000,
"TaxFormattedPrice": "$0.00",
"TotalPrice": 0.00000000,
"TotalFormattedPrice": "$0.00",
"Price": 0.0000,
"FormattedPrice": "$0.00"
}]
}]
},
{
"OrderLineId": "15049017874163171463",
"Quantity": 8,
"UnitPrice": 40.0000,
"TaxPercent": 2.0000,
"UnitFormattedPrice": "$40.00",
"UnitTaxPrice": 0.80000000,
"UnitTaxFormattedPrice": "$0.80",
"TaxRate": 0.0200,
"TaxPrice": 6.4000,
"TaxFormattedPrice": "$6.40",
"TotalPrice": 326.40,
"TotalFormattedPrice": "$326.40",
"TotalPriceWithoutTax": 320.0000,
"TotalFormattedPriceWithoutTax": "$320.00",
"Price": 40.0000,
"FormattedPrice": "$40.00",
"Type": "ModuleItem",
"ModuleId": 2034,
"UnitTotalPrice": 40.80,
"UnitTotalFormattedPrice": "$40.80",
"SKUCode": "65d1be86-2aed-46c0-a402-bd031f033a3e",
"Url": "/catalog/product-name-1-1-1-1-1",
"TaxCode": null,
"UnitRecommendedPrice": 0.0000,
"UnitRecommendedFormattedPrice": "$0.00",
"UnitRecommendedTaxPrice": 0.00000000,
"UnitRecommendedTaxFormattedPrice": "$0.00",
"UnitRecommendedTotalPrice": 0.00000000,
"UnitRecommendedTotalFormattedPrice": "$0.00",
"OnSale": false,
"Attributes": [{
"Id": "12462596046050033672",
"Name": "Color",
"AttributeType": 0,
"Price": 0.0000,
"FormattedPrice": "$0.00",
"TaxPrice": 0.00000000,
"TaxFormattedPrice": "$0.00",
"TotalPrice": 0.00000000,
"TotalFormattedPrice": "$0.00",
"Options": [{
"Id": "16234894512508370989",
"Name": "Blue",
"TaxPrice": 0.00000000,
"TaxFormattedPrice": "$0.00",
"TotalPrice": 0.00000000,
"TotalFormattedPrice": "$0.00",
"Price": 0.0000,
"FormattedPrice": "$0.00"
}]
}]
},
{
"OrderLineId": "15049017874163171464",
"Quantity": 7,
"UnitPrice": 40.0000,
"TaxPercent": 2.0000,
"UnitFormattedPrice": "$40.00",
"UnitTaxPrice": 0.80000000,
"UnitTaxFormattedPrice": "$0.80",
"TaxRate": 0.0200,
"TaxPrice": 5.6000,
"TaxFormattedPrice": "$5.60",
"TotalPrice": 285.60,
"TotalFormattedPrice": "$285.60",
"TotalPriceWithoutTax": 280.0000,
"TotalFormattedPriceWithoutTax": "$280.00",
"Price": 40.0000,
"FormattedPrice": "$40.00",
"Type": "ModuleItem",
"ModuleId": 2034,
"UnitTotalPrice": 40.80,
"UnitTotalFormattedPrice": "$40.80",
"SKUCode": "65d1be86-2aed-46c0-a402-bd031f033a3e",
"Url": "/catalog/product-name-1-1-1-1-1",
"TaxCode": null,
"UnitRecommendedPrice": 0.0000,
"UnitRecommendedFormattedPrice": "$0.00",
"UnitRecommendedTaxPrice": 0.00000000,
"UnitRecommendedTaxFormattedPrice": "$0.00",
"UnitRecommendedTotalPrice": 0.00000000,
"UnitRecommendedTotalFormattedPrice": "$0.00",
"OnSale": false,
"Attributes": [{
"Id": "12462596046050033672",
"Name": "Color",
"AttributeType": 0,
"Price": 0.0000,
"FormattedPrice": "$0.00",
"TaxPrice": 0.00000000,
"TaxFormattedPrice": "$0.00",
"TotalPrice": 0.00000000,
"TotalFormattedPrice": "$0.00",
"Options": [{
"Id": "16234894512508370985",
"Name": "White",
"TaxPrice": 0.00000000,
"TaxFormattedPrice": "$0.00",
"TotalPrice": 0.00000000,
"TotalFormattedPrice": "$0.00",
"Price": 0.0000,
"FormattedPrice": "$0.00"
}]
}]
}
],
"ShippingOption": {
"ShippingOptionId": null,
"Name": null,
"TaxPrice": 0.0,
"TaxFormattedPrice": "$0.00",
"TotalPrice": 0.0,
"TotalFormattedPrice": "$0.00",
"Price": 0.0,
"FormattedPrice": "$0.00",
"DiscountPrice": 0.0,
"DiscountFormattedPrice": "$0.00",
"TaxRate": 0.0200,
"TaxPercent": 2.0000
},
"Discount": {
"DiscountId": null,
"Amount": 0.0,
"FormattedAmount": "$0.00",
"Code": null,
"Type": "FixedAmount"
},
"GiftVoucher": {
"Amount": 0.0,
"FormattedAmount": "$0.00",
"Balance": 0.0,
"FormattedBalance": "$0.00",
"Name": null
},
"DestinationCountry": {
"Code": "US",
"DisplayName": "UNITED STATES"
},
"DomainCountry": {
"Code": "US",
"DisplayName": "UNITED STATES"
},
"FormatSetting": {
"Name": "Default",
"DecimalsQuantity": 2,
"Culture": "en-US",
"CurrencyModel": {
"Id": "12527484287403950218",
"Code": "USD",
"DigitalCode": "840",
"Name": "US Dollar",
"Sign": "$"
}
},
"ShippingOptions": [{
"Price": 5.0000,
"FormattedPrice": "$5.00",
"IsUserDefined": true,
"Id": "7361027874612051969",
"Name": "Shipping"
}],
"TaxCodes": [{
"Id": "10696064456628109313",
"TaxCode": "Tax Code",
"CountryId": "13700469718849159393",
"Rate": 2.0000
}],
"ShippingControlProperties": [],
"DeliveryPostalCode": null,
"DeliveryCity": null,
"TaxCode": "Tax Code"
}
"Trigger" Events
"Trigger" events can be used in order to perform ecommerce actions via Javascript instead of actual clicks or selected actions on HTML elements.
Example usage:
var trigger = new Event("CMS_TriggerAddToCart");
trigger.data = {
"param": "value"
};
document.dispatchEvent(trigger);
All available "Trigger" events with their expected data are listed below.
CMS_TriggerAddToCart
Fires the AddToCart event along with your configured data.
Send data:
{
"ProductId": int, (required)
"Quantity": int, (required)
"BuyNow": bool, (required)
"ProductAttributes": [
{
"AttributeId": "string",
"OptionsPointers": ["string"]
}
]
}
CMS_TriggerRemoveFromCart
Fires the RemoveFromCart event along with your configured data.
Send data:
{
"CartLineId": "string" (required)
}
CMS_TriggerClearCart
Fires the ClearCart event (no send data applicable).
Send data:
null
CMS_TriggerChangeAttributeOption
Fires the ChangeAttributeOption event along with your configured data.
Send data:
{
"ProductId": "string", (required)
"AttributeId": "string" (required)
}
CMS_TriggerChangeGroupedProduct
Fires the ChangeGroupedProduct event along with your configured data.
Send data:
{
"SourceProductId": int, (required)
"TargetProductId": int, (required)
"LayoutName": "string", (required)
"ProductHolder": domElement
}
If ProductHolder
specified, replace layout only for that holder. Otherwise, replace all holders of the SourceProduct
.
CMS_TriggerChangeProductQuantity
Fires the ChangeProductQuantity event along with your configured data.
Send data:
{
"CartLineId": "string", (required)
"Quantity": int (required)
}
CMS_TriggerChangeDestinationCountry
Fires the ChangeDestinationCountry event along with your configured data.
Send data:
{
"Value": "string" (required)
}
CMS_TriggerChangeShippingOption
Fires the ChangeShippingOption event along with your configured data.
Send data:
{
"Value": "string" (required)
}
CMS_TriggerChangeGiftVoucher
Fires the ChangeGiftVoucher event along with your configured data.
Send data:
{
"Value": "string" (required)
}
CMS_TriggerChangeDiscount
Fires the ChangeDiscount event along with your configured data.
Send data:
{
"Value": "string" (required)
}
CMS_TriggerChangeTaxCode
Fires the ChangeTaxCode event along with your configured data.
Send data:
{
"Value": "string" (required)
}
CMS_TriggerGoToCheckout
Fires the GoToCheckout event (no send data applicable).
Send data:
null