在OpenCart中,我们如何实时更新数据

In OpenCart, how can we update the data in real time

本文关键字:实时更新 数据 我们 OpenCart      更新时间:2024-04-08

好吧,我是OpenCart的新手。最近开始在Opencart文件系统中处理代码。我的目标是在页眉上显示cart-div,以显示在其他部分,如公共右侧或页脚。因此,在看了header.php和header.tpl之后,我做了一些更改。我在页脚.php上写了-

$data['cart'] = $this->load->controller('common/cart');

在footer.tpl上,我只是简单地回应了$cart。

但结果并不是我想要的。看看一些屏幕截图-

http://postimg.org/image/7v77iswgz/

http://postimg.org/image/thi05wgfj/

所以我的问题是,我必须做什么才能在不刷新页面的情况下更新总价。如果你能额外提供任何链接,我就可以从中学习用代码自定义opencart,那就太好了。

请检查common.js文件并修改var cart以在页脚中添加数据。

var cart = {
    'add': function(product_id, quantity) {
        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
            dataType: 'json',
            beforeSend: function() {
                $('#cart > button').button('loading');
            },
            success: function(json) {
                $('.alert, .text-danger').remove();
                $('#cart > button').button('reset');
                if (json['redirect']) {
                    location = json['redirect'];
                }
                if (json['success']) {
                    $('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                    $('#cart-total').html(json['total']);
                    $('html, body').animate({ scrollTop: 0 }, 'slow');
                    $('#cart > ul').load('index.php?route=common/cart/info ul li');
                }
            }
        });
    },
    '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',
            beforeSend: function() {
                $('#cart > button').button('loading');
            },
            success: function(json) {
                $('#cart > button').button('reset');
                $('#cart-total').html(json['total']);
//add your footer button here
                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');
                }
            }
        });
    },
    'remove': function(key) {
        $.ajax({
            url: 'index.php?route=checkout/cart/remove',
            type: 'post',
            data: 'key=' + key,
            dataType: 'json',
            beforeSend: function() {
                $('#cart > button').button('loading');
            },
            success: function(json) {
                $('#cart > button').button('reset');
                $('#cart-total').html(json['total']);
//Add your footer button here
                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');
                }
            }
        });
    }
}