Skip to content

Instantly share code, notes, and snippets.

@RuslanSushko
Forked from adardesign/buyNow.js
Last active December 25, 2015 16:18
Show Gist options
  • Save RuslanSushko/958f5e0b820bad5aec88 to your computer and use it in GitHub Desktop.
Save RuslanSushko/958f5e0b820bad5aec88 to your computer and use it in GitHub Desktop.
(function () {
adrma.session.getSession.done(function gotSession(session) {
var buyNowButton,
productInfoContainer = $(".product-info-container");
if (session.cartCount === 0) {
productInfoContainer.find(".add-to-cart").after('<button class="radius add-to-cart button primary-dark action" data-action="silentAddToCart" type="button">Buy Now</button>');
buyNowButton = productInfoContainer.find("[data-action='silentAddToCart']");
if (productInfoContainer.find(".vipProduct").length && !session.isVip) {
buyNowButton.attr({
"rel": "vipLoginPop",
"href": "/sup/static/ADO/viplogin.html",
"viploginpopbutton": "true",
"data-popup-class": "popup300 vipLoginPop sku-" + $(".main-product-section").attr("data-sku"),
"data-tooltip-content": "This purchase is exclusive to existing VIP members only."
});
buyNowButton.addClass("popup hoverInfo").removeClass("action");
}
}
});
var failHandler = function (response) {
if (response.messages) {
adrma.notify.renderApiMessages(response);
}
else {
adrma.notify.add({
html: "Sorry, There was a error adding this item to the cart"
});
}
};
var updateBuyNowButton = function (data) {
var buyNowButton = $(".product-info-container").find("[data-action='silentAddToCart']");
if (data.cartCount && buyNowButton.length) {
buyNowButton.remove();
}
}
adrma.actions.add({
silentAddToCart: function (e, jThis) {
adrma.atc.addToCart(jThis, {
silent: true
}).done(function (response) {
if (response.status === "success") {
location.href = "https://www.adorama.com/als.mvc/nspc/revised";
}
else {
failHandler(response)
}
}).fail(function (response) {
failHandler(response);
});
}
});
/*unified add to cart subscribe*/
$.subscribe("cart/addedItem", function (e, response) {
updateBuyNowButton(response.data);
});
/*AD add to cart subscribe*/
$.subscribe("addToCart", function addToCarthandler(e, data) {
updateBuyNowButton(data);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment