LindsayKB
2/2/2022 - 1:12 AM

Redirect User to Shopify's Checkout or Third-Party Checkout Platform Based on Cart Items

<form action="/checkout" style="display: none;">
    <a href="/checkout" name="checkout" style="display: none;"></a>
</form>
<button id="buy_now_button">BUY NOW</button>
var buyNow = document.getElementById('buy_now_button');
buyNow && buyNow.addEventListener('click', function (evt) {
    evt.preventDefault();
    jQuery.getJSON('/cart.js', function(cart) {
       // now have access to Shopify cart object
       var source = "CartHook";
       // loop through cart
           for (i = 0; i < cart.items.length; i++) {
              var currentID = cart.items[i].product_id;
              //If current product ID matches the desired product ID, go to Shopify
              if (currentID == "PRODUCT ID") {
                   source = "Shopify";
                   break;
              }
           }
      console.log(source);
      //If Product ID is not found, redirect to CartHook
       if (source == "CartHook") {
           window.testGlobalBeforeReact(0);
       }
    
       else if (source == "Shopify") {
           window.location.href ='/checkout';
        }
} );
});