Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

shopify bypass cart

// 1. remove "product-form__cart-submit" class from add to cart form to prevent default behaviour
// 2. Apply custom form submit function:
var byPassCheckout = (function () {
    let addToCartForm = document.querySelector('form[action$="/cart/add"]');
    addToCartForm?.addEventListener('submit', function(e) {
        e.preventDefault();
        let formData = new FormData(addToCartForm);
        fetch(window.Shopify.routes.root + 'cart/add.js', {
            method: 'POST',
            body: formData
        })
        .then(response => {
            window.location.href = '/checkout';
            console.log(response)
            return response.json();
        })
        .catch((error) => {
            console.error('Error:', error);
        });
    });
})();
 
PREVIOUS NEXT
Tagged: #shopify #bypass #cart
ADD COMMENT
Topic
Name
4+7 =