Scriptable cart – To set special price level for the items in the website

The pricing level of the items on the website is specified in the NetSuite website record by default in SCA. If the user is logged in and has a pricing level in their customer record, that price level will be used.

However, we may customize it by changing the price level of some of the items.

In this case, we must change the price level of the special items (the special field is checked in the item record) to the special price level (custom pricing level). Using the client script, we can alter the pricing level from cart to checkout to sales order.

Note: As we have some limitation to fetch the price levels and quantity levels for the item with quantity pricing, we will keep an object with special price level and quantity level in the custom item field.

For example:

{“1-9″:”22.00″,”20+”:”20.00″,”10-19″:”21.00″}

Client script

/**
 * Script Description
 * This script will update the item price level for the special item based on custom item field 'Special'
 */
/*******************************************************************************
 * CLIENTNAME:Pure Care
 * **************************************************************************
 *
 * Date: 12-12-2023
 *
 * Author: Jobin & Jismi IT Services LLP
 *
 *
 * REVISION HISTORY
 *
 *****************************************************************************
 **/


 var jjContext;
 function ClientPageInit(type) {
     jjContext = nlapiGetContext().getExecutionContext();
     if (jjContext == 'webstore' && type == 'item') {
         try {
             customRecalc(type);
         } catch (error) {
             nlapiLogExecution('DEBUG', 'jjWebStore_ClientValidateLine', error);
         }
     }
     return true;
 }
 
 function jjWebStore_ClientValidateLine(type) {
     if (jjContext == 'webstore' && type == 'item') {
         try {
             customRecalc(type);
         } catch (error) {
             nlapiLogExecution('DEBUG', 'jjWebStore_ClientValidateLine', error);
         }
     }
     return true;
 }
 
 function mapPriceFromQuantity(selectedQuantity, array1, array2) {
     for (var i = 0; i < array2.length; i++) {
         var range = array2[i];
         var bounds = range.split('-');
         var lowerBound = parseInt(bounds[0]);
         var upperBound = bounds.length === 2 ? parseInt(bounds[1]) : Infinity;
 
         if ((selectedQuantity >= lowerBound) && (selectedQuantity <= upperBound)) {
             return array1[i];
         }
     }
 
     // Default to the last price in array1 if no match is found
     return array1[array1.length - 1];
 }
 
 function customRecalc(type) {
 
     try {
         var itemtype = nlapiGetCurrentLineItemValue('item', 'itemtype');
 
         if (itemtype === "InvtPart" || itemtype === "NonInvtPart") {
             var ItemDet = nlapiGetCurrentLineItemValue('item', 'item');
 
             var quantity = nlapiGetCurrentLineItemValue('item', 'quantity');
 
             var specialQtyLevels = [];
             var specialPriceLevels = [];
             var hasQtyPricing = false;
 
             if (ItemDet) {
                 var domainUrl = 'https://www.scatest433.tk/';
                 var subsidiary = nlapiGetSubsidiary();
                 subsidiary = !!subsidiary ? subsidiary : 1;
                 nlapiLogExecution('DEBUG', 'subsidiaryk', subsidiary);
                 var fullUrl = domainUrl + 'api/items?id=' + ItemDet + '&region=' + subsidiary + '&fields=pricelevel26,custitem_special,pricelevel33,custitem_jj_show_msrp_price,custitem_special_price_obj';
 
                 var headers = {
                     "Content-Type": "application/json",
                     "Accept": "application/json"
                 };
                 var response = nlapiRequestURL(fullUrl, null, headers, "GET");
                 var body = JSON.parse(response.getBody());
                 var code = response.getCode();
                 var isSpecialItem = body.items[0].custitem_special;
                 var isMSRPItem = body.items[0].custitem_jj_show_msrp_price;
 
                 var specialObj = body.items[0].custitem_special_price_obj;
                 specialObj = JSON.stringify(specialObj);


                 var specialObjArray = specialObj.replace(/:/g, ',');
                 specialObjArray = specialObjArray.split(",")
 
                 if (specialObjArray.length > 1) {
                     for (var i = 0; i < specialObjArray.length; i++) {
                         // Remove extra double quotes from each element
                         var cleanedElement = specialObjArray[i].replace(/[{}"]/g, '');
 
                         if (i % 2 === 0) {
                             // Even index, add to evenArray
                             specialQtyLevels.push(cleanedElement);
                         } else {
                             // Odd index, add to oddArray
                             specialPriceLevels.push(cleanedElement);
                         }
                     }
                     hasQtyPricing = true;
                     var specialQtyPrice = mapPriceFromQuantity(parseInt(quantity), specialPriceLevels, specialQtyLevels);                  
                 }
 
 
                 if (!!isSpecialItem) {
                     if (code == 200 && body.items.length && (body.items[0].pricelevel26)) {
                         var specialPricelevel = hasQtyPricing ? specialQtyPrice : body.items[0].pricelevel26;
                         nlapiSetCurrentLineItemValue('item', 'rate', specialPricelevel);
                     }
                 } else {
                     if (!!isMSRPItem) {
                         if (code == 200 && body.items.length && (body.items[0].pricelevel33)) {
                             var MSRPPricelevel = body.items[0].pricelevel33;
                             nlapiSetCurrentLineItemValue('item', 'rate', MSRPPricelevel);
                         }
                     }
                 }
 
             }
         }
 
     } catch (e) {
         nlapiLogExecution('DEBUG', 'Example Scriptable Cart', e);
 
     }
 
 
 }
 

script record

Leave a comment

Your email address will not be published. Required fields are marked *