Skip to content

Instantly share code, notes, and snippets.

@adardesign
Forked from RuslanSushko/buyNow.js
Last active December 28, 2015 23:24
Show Gist options
  • Save adardesign/17c7e3c629a30db13965 to your computer and use it in GitHub Desktop.
Save adardesign/17c7e3c629a30db13965 to your computer and use it in GitHub Desktop.
(function() {
adrma.session.getSession.done(function gotSession(session) {
var buyNowButton,
productInfoContainer = $(".product-info-container");
if (productInfoContainer.find(".vipProduct").length) {
//WEB-31841
return;
}
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']");
}
});
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