Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Количество товаров в сплывающей корзине


Recommended Posts

Подскажите как исправить ошибку. Хочу добавить возможность изменять количество товаров после добавления в корзину (фото прикреплено). 

В файл cart.twig добавлена разметка:

<td>
    <div class="cart_quantity_button">
      
        <a style="font-size:20px; cursor:pointer" class="cart_quantity_down" data-key="{{ product.cart_id }}">-</a>

        <input class="cart_quantity_input" type="text" name="quantity" value="{{ product.quantity }}" autocomplete="off" size="2" readonly>

        <a style="font-size:20px; cursor:pointer" class="cart_quantity_up" data-key="{{ product.cart_id }}">+</a>
      
    </div>
</td>

 

И в файл common.js добавлено функцию и слушатели:

 

function updateCart(key, quantity) {

    $.ajax({
        url: 'index.php?route=checkout/cart/edit',
        type: 'post',
        data: 'key=' + key + '&quantity=' + (typeof (quantity) != 'undefined' ? quantity : 1),
        dataType: 'json',
        beforeSend: function () {
            $('#cart > button').button('loading');
        },
        complete: function () {
            $('#cart > button').button('reset');

        },
        success: function (data) {

            // Обновление количества товаров в корзине
            $('#cart-total').html(data['total']);

            // Обновление содержимого корзины
            if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
                location = 'index.php?route=checkout/cart';
            } else {

                $('#cart > ul').load('index.php?route=common/cart/info ul li');
            }
        },
        error: function (jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect. Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found (404).');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error (500).');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error. ' + jqXHR.responseText);
            }
        }
    });
}

$('.cart_quantity_up').on('click', function () {
    var key = $(this).data('key');
    var quantity = $(this).parent().find('.cart_quantity_input').val();
    quantity++;
    $(this).parent().find('.cart_quantity_input').val(quantity);

    updateCart(key, quantity);
});

$('.cart_quantity_down').on('click', function () {
    var key = $(this).data('key');
    var quantity = $(this).parent().find('.cart_quantity_input').val();
    if (quantity > 1) {
        quantity--;
        $(this).parent().find('.cart_quantity_input').val(quantity);
        updateCart(key, quantity);
    }
});

 

При нажатии на +/- срабатывает событие, количество увеличивается/уменьшается и вызывается функция updateCart() которая должна обновить содержимое корзины. Но метод success не выполняется, так как получаю ошибку "parsererror". (фото прикреплено)

cart.JPG

error.JPG

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.