Answer the question
In order to leave comments, you need to log in
How to disable caching of a specific url?
I'm making a store on Opencart 3.0.2.0 and faced such a problem. When I add an item to the cart, the information in the mini cart is not updated. Everything works fine in the demo version of Opencart. Everything also works fine with Disable cache enabled in the developer panel.
Here is the JS code that is responsible for adding, updating and removing items from the cart
var cart = {
'add': function(product_id, quantity) {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
cache:false,
data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
dataType: 'json',
beforeSend: function() {
$('#cart > .mini-cart_hook').button('loading');
},
complete: function() {
$('#cart > .mini-cart_hook').button('reset');
},
success: function(json) {
$('.alert-dismissible, .text-danger').remove();
if (json['redirect']) {
location = json['redirect'];
}
if (json['success']) {
$('#content').parent().before('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">×</button></div>');
// Need to set timeout otherwise it wont update the total
setTimeout(function () {
$('#cart > .mini-cart_hook').html('<i class="fal fa-shopping-cart"></i><span id="cart-total">' + json['total'] + '</span>');
}, 100);
$('html, body').animate({ scrollTop: 0 }, 'slow');
$('#cart > ul').load('index.php?route=common/cart/info ul li');
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
},
'update': function(key, quantity) {
$.ajax({
url: 'index.php?route=checkout/cart/edit',
type: 'post',
data: 'key=' + key + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
cache: false,
dataType: 'json',
beforeSend: function() {
$('#cart > .mini-cart_hook').button('loading');
},
complete: function() {
$('#cart > .mini-cart_hook').button('reset');
},
success: function(json) {
// Need to set timeout otherwise it wont update the total
setTimeout(function () {
$('#cart > .mini-cart_hook').html('<i class="fal fa-shopping-cart"></i><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 > ul').load('index.php?route=common/cart/info ul li');
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
},
'remove': function(key) {
$.ajax({
url: 'index.php?route=checkout/cart/remove',
type: 'post',
data: 'key=' + key,
cache: false,
dataType: 'json',
beforeSend: function() {
$('#cart > .mini-cart_hook').button('loading');
},
complete: function() {
$('#cart > .mini-cart_hook').button('reset');
},
success: function(json) {
// Need to set timeout otherwise it wont update the total
setTimeout(function () {
$('#cart > .mini-cart_hook').html('<i class="fal fa-shopping-cart"></i><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 > ul').load('index.php?route=common/cart/info ul li');
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}
}
$('#cart > ul').load('index.php?route=common/cart/info ul li');
Answer the question
In order to leave comments, you need to log in
Thanks to everyone who tried to help! Solved the problem in the most proven way, started from scratch commt'I every step!
Ctrl+F5 in browser doesn't help?
In general, caching is disabled on the server side when generating a response to the client.
<?php
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
?>
But what if you try to add a fake parameter to the address? For example index.php?route=common/cart/info&rand=random_value . Will it work?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question