Answer the question
In order to leave comments, you need to log in
How to correctly send an Ajax request to update the quantity of goods on the opencart 3 cart page?
I'm trying to update the number of items on the shopping cart page via Ajax. There is a lot of information on the Internet on the topic, but each of them turned out to be ineffective.
Each item in the cart has a quantity field . For me, it looks like this
<input type="text" name="quantity[{{ product.cart_id }}]" value="{{ product.quantity }}" size="1" min="1" class="tac ch_quantity" oninput="updateCart('{{ product.quantity }}', '{{ product.cart_id }}', $(this).val());" />
size="1" и oninput="updateCart('{{ product.quantity }}', '{{ product.cart_id }}', $(this).val());"
function updateCart(quantity, cart_id, val) {
setTimeout(function(){ // ждем 100 миллисекунд
//alert('quantity = '+ quantity +', cart_id = ' + cart_id + ', val = ' + val);
if (quantity != 0 && quantity != 'undefined') { // если кол-во не равно 0, то делаем ajax запрос
$.ajax({
url: 'index.php?route=checkout/cart/edit', // на этот урл
type: 'post', //пост
//data: 'cart_id['+cart_id+']='+val, //пробовал сначала этот вариант data
data: 'key=' + cart_id + '&quantity=' + (typeof(val) != 'undefined' ? val : 1), // потом этот как из коробки в common.js
dataType: 'json', //json
success: function(json) {
//да, тут кастомизированная корзина, тоесть все тоже самое что из коробки только заместо ul, li, table сделал div-"ы" (суть не меняется от этого)
$('#cart .ch_allProductsWrapper').load('index.php?route=common/cart/info .ch_allProductsWrapper .ch_products'); // обновляем корзину в шапке
if($('#checkout-cart').length>0){
$('#checkout-cart .ch_allProductsWrapper').load('index.php?route=common/cart/info .ch_allProductsWrapper .ch_products'); // обновляем на странице корзина
}
},
error: function(xhr, ajaxOptions, thrownError) {
$.jGrowl(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}
}, 100);
}
'update': function(key, quantity) {
$.ajax({
url: 'index.php?route=checkout/cart/edit',
type: 'post',
data: 'key=' + key + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
dataType: 'json',
success: function(json) {
// Need to set timeout otherwise it wont update the total
setTimeout(function () {
$('header .headerTotal').html('<span id="cart-total">' + json['total'] + '</span>');
}, 100);
if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') {
location = 'index.php?route=checkout/cart';
} else {
$('#cart .ch_allProductsWrapper').load('index.php?route=common/cart/info .ch_allProductsWrapper .ch_products');
if($('#checkout-cart').length>0){
$('#checkout-cart .ch_allProductsWrapper').load('index.php?route=common/cart/info .ch_allProductsWrapper .ch_products');
}
}
},
error: function(xhr, ajaxOptions, thrownError) {
$.jGrowl(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
},
Answer the question
In order to leave comments, you need to log in
As I understand it, the page there is
still updated by the controller, and since the value="{{
product.quantity
}}" values
are not updated to new ones after update works.
If anything, contact me in the cart, I will help, how I will finish this question.
The thing is...
open
the cpmmon/cart controller
AND you will see SOMETHING
$data['text_items'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() +
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question