Skip to content

Instantly share code, notes, and snippets.

@msanigar
Created September 29, 2015 15:04
Show Gist options
  • Save msanigar/f61e630b95299b82ab4d to your computer and use it in GitHub Desktop.
Save msanigar/f61e630b95299b82ab4d to your computer and use it in GitHub Desktop.
check the previously generated basket url for out of stock products when building the site basket
/*
* app -> site basket item validation
* alert user if sku's missing, presumed out of stock
/
/*
* example of multi with missing sku: ?log=22&ex=co_wizr-shopcart&mode=addmul&param1=bulk&qtylist=2&itemlist=azz1368510520&qtylist=1&itemlist=azz2199912835&itemlist=azz111111&itemlist=azz111112
* example of single sku ?log=22&ex=co_wizr-shopcart&mode=addmul&param1=bulk&qtylist=2&itemlist=azz1368510520&qtylist=1&itemlist=azz2199912835
* example of multi sku ?log=22&ex=co_wizr-shopcart&mode=addmul&param1=bulk&qtylist=1&itemlist=azz1368510520&
*/
/*
* first we need to check that we're in the shopcart
*/
$( document ).ready(function(){
var _wfshop = false,
_wfitems = false;
if ( $('.workflow-shopcart').length ) {
_wfshop = true;
}
/*
* now we need to check if this is app -> site flow
*/
if (window.location.href.indexOf("itemlist") > -1) {
_wfitems = true;
}
/*
* if both are true, we know what we're dealing with
*/
if ( _wfshop && _wfitems ) {
compareItems();
}
/*
* now we need to compare the items passed in the url
* with those that have actually added to basket
* any that are missing presume out of stock
* display a warning for the user
*/
function compareItems() {
/*
* get skus from url
*/
var itemArr = location.search.substring(1).split( '&' ).reduce(function( pre, cur ){
var _param = cur.split( '=' );
if ( _param[0] === 'itemlist' ) pre.push( _param[1] );
return pre;
},[]);
/*
* array of skus from url itemArr, thanks James!
*/
/*
* get skus from basket
*/
var _basket = _orditemJSON(),
basketArr = [],
missingSkus = [];
var _basketArr = $.each(_basket, function( index ){
basketArr.push( this.sku );
});
/*
* check if string of basket skus matches array of url skus (itemArr & basketArr)
* return any not matching
*/
$.grep( itemArr, function( el ) {
if ( $.inArray( el, basketArr ) == -1 ) missingSkus.push( el );
});
/*
* alert the user in nice big bold red text :)
*/
$( "<div class=\"row\"><strong class=\"classRed\" style=\"text-align:center;display:block;\">The following items are now out of stock, and could not be added to the basket: " + missingSkus + "</strong>" ).insertAfter( ".js-header-promobasket-2-div" );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment