Skip to content

Instantly share code, notes, and snippets.

@adardesign
Last active November 23, 2015 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adardesign/4d33e25ec29cb8c2294d to your computer and use it in GitHub Desktop.
Save adardesign/4d33e25ec29cb8c2294d to your computer and use it in GitHub Desktop.
(function(){
var session = adrma.storage.get("local", "session");
if (!session.cartCount > 0 ) {
$(".product-info-container .add-to-cart").after('<button class="radius add-to-cart button primary-dark action" data-action="buyNow" type="button">Buy Now</button>');
}
adrma.actions.add({
buyNow: function(e, jThis) {
var formEle = jThis.closest("form");
//Form
if (!formEle.length) {
// what if it needs to be logged-in
var jThis = formEle,
inputs = jThis.find("input"),
atcEle = jThis.find("input[type='submit'], .AddToCart"),
data = {},
postData,
storage = adrma.storage,
alreadyGotSku = false,
maxQty = jThis.find("[data-maxqty]");
// test if its a login required... if yes, just return
if (jThis.find("[name = 'nextpage']").length) {
return true;
}
// limit qty NEWWEB-11434
if (maxQty.length) {
var qty = maxQty.attr("data-maxqty");
maxQty.removeClass("error");
jThis.find(".qtyLimitError").remove();
if (+maxQty.val() > qty) { // converting val to Number
maxQty.addClass("error");
jThis.find(".quantity").parent('div').append("<span class='error message qtyLimitError'>There is a limit of qty that can be purchased, the max quantity is " + qty + "</span>");
return false;
}
}
$.each(inputs.get(), function(i, input) {
var name = input.name,
sku;
if (/_qty$/.test(name)) {
sku = name.match(/(.+?)\_qty/); // was -- NEWWEB-12568 (/(.+?)\_(.+)/)
if (sku[1].indexOf("_r") > 0) { // a rebate one looks like this "SKU_rRebateNum_qty"
if (alreadyGotSku) { // only add first one... cuase the free item shouldnt be added... NEWWEB-10904 - NEWWEB-12568
return true; // skip to next input NEWWEB-10904
}
sku[1] = sku[1].split("_")[0]; // take the value befor the _r
alreadyGotSku = true;
}
sku = sku.length && sku[1] ? sku[1] : "";
data[sku] = {
"qty": input.value
};
}
});
} else {
//inpit
data = adrma.atc.getProductData(formEle.closest("[data-sku]").attr("data-sku"));
}
var date = new Date();
postData = {
clientTimeStamp: (date.getTime() - (date.getTimezoneOffset() * 60000)),
cartItems: data,
type: "miniCart",
isEmailPrice: self.emailPrice
};
if ($.isEmptyObject(data)) {
return false;
}
adrma.fetchData({
url: "/api/order/addToCart",
data: data,
type: "POST",
contentType: "json",
dataType: "json"
}).done(function(response) {
if (response.status === "success") {
location.href = "https://www.adorama.com/als.mvc/nspc/revised";
}
}).fail(function(response) {
adrma.notify.add({
html: "Sorry, There was a error adding this item to the cart"
});
});
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment