Is Residential Flag
Hi All.
there've been various threads about the "is residential" flag for web orders.
I recently had a new opportunity to implement this using scriptable checkout and now we actually get live shipping cost updates!
The parts:
A transaction body custom checkbox field "Residential Shipping Address"
Scriptable Checkout
Some script injection in the site theme footer. (this was for a WSDK site. put a blank tag in the footer of a regular site and override it for the check out tab for a regular site)
The checkbox:
regular checkbox field with web site checked. Script id is:
custbody_residential_shipping_address
some generally useful code for your site header:
[CODE]
<style type="text/css">
.initHide{display:none;}
</style>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <!-- uses http or https as appropriate -->
[/CODE]
The script injection (this is code added to the site footer below your last visual elements. The javascript relies on jQuery):
[HTML]
<div id="residential_disclaimer" class="initHide"><RESIDENTIAL_FLAG_NOTICE></div> <!-- warns of a $15 surcharge for inaccuracy -->
<script type="text/javascript">
function forceRefresh(){setTimeout(function(){ location.reload(true);}, 800); return true;} // need to reload to see the changed rate
var requiredIcon = '<img src="/images/chiles/pageTitle/required.png" title="Required Field" class="required_icon">';
(function(){ // apply checkout page customizations
var x = document.forms['checkout'];
if(x){
if(!x.custValidations) x.custValidations = {};
var origCheck = null;
var $ = jQuery;
$("#custbody_residential_shipping_address_fs_lbl").before(requiredIcon); // not really required but want some visual weight
var myFs = $("#custbody_residential_shipping_address_fs");
if(myFs.length){
myFs.closest('tr').find('td').attr('valign', 'top');
myFs.css('float', 'left').after($("#residential_disclaimer").removeClass('initHide'));
$("#custbody_residential_shipping_address_fs_inp").bind('change', forceRefresh);
}
...
}
})();
</script>
[/HTML]
the scriptable cart field changed function:
[CODE]
var scriptableCart = new (function(){
function tty(type, title, detail){ nlapiLogExecution(type, title, (detail || ''));}
this.fieldChanged = function(type, name, linenum){
if(name == 'custbody_residential_shipping_address'){
var isResidential = nlapiGetFieldValue('custbody_residential_shipping_address');
if(!isResidential) return; // not set yet.
tty('DEBUG', 'is Residential: '+ isResidential);
tty('DEBUG', 'have ship address id: '+ shipAddrId);
try{
if(isResidential !== nlapiGetFieldValue('shipisresidential')){
tty('DEBUG', 'before residential: '+ nlapiGetFieldValue('shippingcost'));
nlapiSetFieldValue('shipisresidential', isResidential, true, true);
var currentShipMethod = nlapiGetFieldValue('shipmethod');
nlapiSetFieldValue('shipmethod', null, true, true);
nlapiSetFieldValue('shipmethod', currentShipMethod, true, true);
tty('DEBUG', 'after residential: '+ nlapiGetFieldValue('shippingcost'));
}
}catch(e){
tty("ERROR", 'updating ship address', (e.message || e.toString()) + (e.getStackTrace ? '\n\n'+ e.getStackTrace().join('\n') : ''));
}
}
};
});
[/CODE]