buy-button-js
buy-button-js copied to clipboard
Manually call the checkout-event?
I am using Buy-Button-JS to add e-commerce functionality to a site. Everything works as expected.
However, I would like to call the checkout (the same function that is called when the »Checkout« button in the cart is clicked) manually, i.e. call it with another button, somewhere else on the page. Is it possible to trigger the checkout manually like that?
Thanks for any help.
You can capture the cart
in its beforeInit
event and then call cart.onCheckout
:
var shopifyClient = ShopifyBuy.buildClient({});
var shopifyBuyUI = ShopifyBuy.UI.init(shopifyClient);
shopifyBuyUI.createComponent("", {
options: {
cart: {
events: {
beforeInit: function (cart) {
var myButton = document.querySelector("#my-button");
myButton.addEventListener("click", function () {
cart.onCheckout();
});
}
}
}
}
});