-1

Okay so,I am pretty a newbie in OpenCart. Recently started working on the code inside the Opencart file system. My objective is to display the cart div on the header to display on some other part like common right or footer. So after taking a look on header.php and header.tpl I made some changes. On the footer.php I wrote-

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

And on the footer.tpl I simply echoed out $cart.

But the result is not what I was looking for. Take a look at some of the screenshots-

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

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

So my question is that, what must I do to make the updation of total price without refreshing the page. It would be great if you can additionally provide any link from where I can learn to customize the opencart with codes.

shadyyx
  • 15,825
  • 6
  • 60
  • 95
Akansha
  • 1
  • 4
  • You need to use ajax while calling common/cart controller method. While clicking on add to cart button you need to fire your JS function which contains this ajax call. – Lalit Sharma Feb 24 '15 at 06:11
  • You can call any js function on clicking on any button's event using ON event in jquery. – Lalit Sharma Feb 24 '15 at 06:18

1 Answers1

1

Please check common.js file and modify var cart to add data in your footer as well.

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');
                }
            }
        });
    }
}
shahmanthan9
  • 473
  • 1
  • 3
  • 14
  • Hi thanks for the answer, but I am still stumbling on adding the footer button too. Can you please guide me how to write that syntactically please. – Akansha Feb 24 '15 at 10:20
  • Track ajax call of product adding to cart in firebug/developer tool. Once that's done, check the response of it in firebug/developer tool. Once you see that you will have complete idea of what to do next. Also put various `console.logs` in code shown. – shahmanthan9 Feb 24 '15 at 10:25