Skip to content

Instantly share code, notes, and snippets.

@ofiesh
Last active August 29, 2015 13:59
Show Gist options
  • Save ofiesh/10920800 to your computer and use it in GitHub Desktop.
Save ofiesh/10920800 to your computer and use it in GitHub Desktop.
var $stockCount,
$leadTimeText,
$stockZero,
$availabilityVaries,
$freeShipping,
pageMessaging = {
'hideUnavailableByLocation': function(){},
'showUnavailableByLocation': function(){}
};
function updateStockMessage(){}
function hasAvailabilityByLocation(){}
function isFinishSelected(){}
function isPostalCodeDefined(){}
function toggleAddToCart(){}
function getSelectedFinish(){}
/*
Refactor below
*/
function updatePageMessaging($swatch) {
var currentSelectedFinish = $swatch || getSelectedFinish(),
stock = getStock(),
hideOutOfStockMessage = currentSelectedFinish.data("hidemessage") === true;
if(hasAvailabilityByLocation())
updateAvailableByLocationStock(currentSelectedFinish, stock, hideOutOfStockMessage);
else
updateAlwaysAvailableStock(stock, hideOutOfStockMessage);
}
function updateAvailableByLocationStock(currentSelectedFinish, stock, hideOutOfStockMessage) {
$availabilityVaries.show();
if(isFinishSelected() && isPostalCodeDefined()) {
$availabilityVaries.find('label').removeClass('prd_error_highlight');
if(currentSelectedFinish.data("isavailable")) {
pageMessaging.hideUnavailableByLocation();
updateStockMessage($stockCount, stock).show();
if(stock > 0) {
$stockZero.hide();
$leadTimeText.show();
toggleAddToCart(true);
} else {
if(stock === 0)
$stockCount.hide();
else if(hideOutOfStockMessage)
$stockCount.text('Out Of Stock').addClass('out-of-stock').show();
toggleAddToCart(false);
}
} else {
pageMessaging.showUnavailableByLocation();
$stockCount.hide();
$stockZero.hide();
$freeShipping.hide();
$leadTimeText.hide();
toggleAddToCart(false);
}
} else {
toggleAddToCart(false);
}
}
function updateAlwaysAvailableStock(stock, hideOutOfStockMessage) {
$leadTimeText.show();
if(stock > 0) {
$stockZero.hide();
updateStockMessage($stockCount, stock).show();
} else {
if(stock === 0) {
$stockCount.hide();
}
hideOutOfStockMessage ? $stockZero.hide() : $stockZero.show();
}
}
function getStock(currentSelectedFinish) {
if (hasAvailabilityByLocation && currentSelectedFinish.data("sku") === undefined) {
currentSelectedFinish = currentSelectedFinish.parent("li").find("div[data-name]");
return parseInt(currentSelectedFinish.data("stock"), 10);
}
return currentSelectedFinish.length ? parseInt(currentSelectedFinish.data('stock'), 10): null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment