L
L
lazuren2018-09-07 22:30:00
opencart
lazuren, 2018-09-07 22:30:00

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. 5b92d101c0f62711531236.png
5b92d10cc39a0307241936.png
5b92d117ae6a5129024143.png

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">&times;</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);
        }
    });
}
}

And here is the direct piece of code itself that should update the information, but the browser pulls it from cache
$('#cart > ul').load('index.php?route=common/cart/info ul li');


Tell me how to deal with it. How to disable caching for a specific URL and get updated data from the database and not from disk cache?

Sorry for the confusion!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lazuren, 2018-09-09
@lazuren

Thanks to everyone who tried to help! Solved the problem in the most proven way, started from scratch commt'I every step!

D
dollar, 2018-09-07
@dollar

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");
?>

S
Station 72, 2018-09-09
@cjstress

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 question

Ask a Question

731 491 924 answers to any question