T
T
trigyo2020-10-18 15:25:02
JavaScript
trigyo, 2020-10-18 15:25:02

Post request to add item to cart not working?

You need to generate links to add the desired product and size to the cart
, 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
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question