Limiting ‘out-of-items’ to cart

The requirement is ‘out-of-stock’ items should not be added to the cart. By using the client script this can be achieved. So ‘out-of-stock items will not be added to the cart from any pages.

clientscript 1.0

/**
 * Script Description
 * This script will update the item line based on promotion
 */
/*******************************************************************************
 * Ox tools
 * **************************************************************************
 *
 * Date: 05-03-2021
 *
 * Author: Jobin & Jismi IT Services LLP
 *
 *
 * REVISION HISTORY
 *
 *****************************************************************************
 **/
var jjContext, entity, promotionPrice = '85';

function ClientPageInit(type) {
  
    jjContext = nlapiGetContext().getExecutionContext();
    nlapiLogExecution('DEBUG', 'SCA', ':: page init');
    try {
        var entity = nlapiGetFieldValue('entity')
    } catch (error) {
        nlapiLogExecution('DEBUG', 'entity', error);
    }

    if (jjContext == 'userinterface') {
        nlapiLogExecution('DEBUG', 'userinterface', 'error');
    }
}


function customOnChange(type, name, linenum) {

    if (jjContext == 'webstore' && type == 'item' && name == 'item') {
        promoPriceRecalc(type, linenum)
    }
}

function jjWebStore_ClientValidateLine(type) {
    //
    if (jjContext == 'webstore' && type == 'item') {
        try {
            nlapiLogExecution('DEBUG', 'ItemDettype', type);
            promoPriceRecalc(type); //
        } catch (error) {
            nlapiLogExecution('DEBUG', 'jjWebStore_ClientValidateLine', error);            
        }

    }
    return true;
}

/**
 * Set correct price; apply column mapping
 * runs on each line validation (on checkout - on back-end)
 */
function promoPriceRecalc(type, linenum) {
    
    if (jjContext == 'webstore' && type == 'item') {
        try {
            var ItemDet = nlapiGetCurrentLineItemValue('item', 'item');
            var quantity = nlapiGetCurrentLineItemValue('item', 'quantity');
            var domainUrl1 = nlapiGetContext();
            var currentlineNum=nlapiGetCurrentLineItemIndex('item')
            var param = nlapiGetContext().getSetting('SCRIPT', 'custscript_otgn_445_domainurl') //nlobjContext.getSetting('SCRIPT', 'custscript_otgn_445_domainurl')           
            var domainUrl = 'https://shop.oxtools.com/';
            var subsidiary = nlapiGetSubsidiary();
            if (ItemDet) {
                var domainUrl = 'https://shop.oxtools.com/';
                var fullUrl = domainUrl + 'api/items?id=' + ItemDet + '&region=' + subsidiary +'&fields=pricelevel85,custitem_usa_promotion,pricelevel175,' +
              'custitem_nz_promotion,pricelevel174,custitem_au_promotions,pricelevel176,custitem_au_promotions_integer,custitem_uk_promotion,quantityavailable';
                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 subsidiary = nlapiGetSubsidiary();
              	var quantityavailable = body.items[0].quantityavailable
                nlapiLogExecution('DEBUG', 'quantityavailable', quantityavailable);
              
              if (quantityavailable>=0) {
                try {                     
                      if(parseInt(quantity) > parseInt(quantityavailable)){                      
                        nlapiSetCurrentLineItemValue('item', 'quantity', quantityavailable);                       
                      }
                    } catch (error) {
                        nlapiLogExecution('DEBUG', 'pricelevel85error123', error);
                    }
              }
              if(parseInt(quantityavailable) === 0){
                try {                             
                if(currentlineNum>0){
                    nlapiRemoveLineItem('item', currentlineNum)
                                }
                } catch (error) {
                nlapiLogExecution('DEBUG', 'outqtyerror', error);
                }
               }

            }

        } catch (error) {
            nlapiLogExecution('DEBUG', 'promoPriceRecalc', error);
        }
        return true;
    }
}
/**
 * Set correct price; apply column mapping
 * runs on each line validation (on checkout - on back-end)
 */

Leave a comment

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