buy-button-js icon indicating copy to clipboard operation
buy-button-js copied to clipboard

Manually call the checkout-event?

Open joh-sch opened this issue 4 years ago • 1 comments

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.

joh-sch avatar Jun 18 '20 12:06 joh-sch

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();
          });
        }
      }
    }
  }
});

gabrielmaldi avatar Sep 17 '20 22:09 gabrielmaldi