Post request to add item to cart not working?

  • 0
    You need to generate links to add the desired product and size to the box.
    here is the site https://brandshop.ru/
    var id = new URL(window.location.href).searchParams.get('id');  // Parse id
            var option = new URL(window.location.href).searchParams.get('option');  // Parse option
            var quantity = new URL(window.location.href).searchParams.get('q');  // Parse quantity
            var option_name = new URL(window.location.href).searchParams.get('option_name');  // Parse quantity
            var option_name_id = new URL(window.location.href).searchParams.get('option_name_id');  // Parse quantity
    
    
    
            if (id && id == parseInt(id) && option && option == parseInt(option)) {  // Validate id & option
                if (!quantity || quantity != parseInt(quantity) || quantity < 1) quantity = 1;  // Validate quantity
    
                fetch('https://brandshop.ru/index.php?route=checkout/cart/add', {  // Add product to cart
                    'body': `product_id=${id}&option_value_id=${option}&option[${option_name}]=${option_name_id}&quantity=${quantity}`,
                    'credentials': 'include',
                    'headers': {
                        'Accepts': '*/*',
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    'method': 'POST',
                    'mode': 'no-cors'
                }).then(function () {
                    window.location.href = 'https://brandshop.ru/checkout/';  // Go to checkout
                });
            } else {
                window.location.href = '/';  // Go to homepage if id & option validation failed
            }
    JavaScript Finn Herring, Nov 22, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!