Post by speedingorange » Tue Sep 19, 2017 7:59 pm

Hi All,

I am working on an adaptation of the stock theme for a slightly more compact view on mobiles. To get everything to display nice I need to show a count of products in the cart outside of the standard id="cart" button.

I want to show a simple count of how many products are in the cart (no monetary total etc, just how many products are in the cart)

I have this working using the following code:

In catalogue/common/header.php

Code: Select all

//how many things in cart
$data['cartcount'] = $this->cart->countProducts();
Then in the header.tpl

Code: Select all

<?php if (isset ($cartcount)) { echo $cartcount; } ?>
this works but, does not offer the functionality of updating as soon as a product is put in the cart. It shows the correct count of products, but only when the next page is loaded.

Would anyone be able to point me in the right direction of how to make this update as soon as anyone hits the "add to cart" button or updates cart totals? I am assuming that something in common.js needs updating? ive played around with it but cannot get it to work still.

I am on opencart 2.1.0.2 if that makes any odds.

Cheers!
James

Active Member

Posts

Joined
Tue Feb 23, 2010 7:33 pm

Post by vaskazz » Fri Jul 13, 2018 10:10 pm

I might want to try this a bit modified version.
In catalogue/common/header.php

Code: Select all

//how many things in cart
$this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0);
In the catalogue/common/header.tpl

Code: Select all

<span id="cart-total"><?php if (isset ($cartcount)) { echo $cartcount; } ?></span>
In catalogue/view/theme/*/template/product/product.tpl find:

Code: Select all

$('#cart > button').html('<i class="fa fa-shopping-cart"></i> ' + json['total']);
add after:

Code: Select all

$('#cart-total').html(json['cartcount']);
In catalogue/controller/checkout/cart.php find:

Code: Select all

$json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total));
add after:

Code: Select all

$json['cartcount'] = $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0);
find:

Code: Select all

$json['total'] = sprintf($this->language->get('text_items'), $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0), $this->currency->format($total));
add after:

Code: Select all

$json['cartcount'] = $this->cart->countProducts() + (isset($this->session->data['vouchers']) ? count($this->session->data['vouchers']) : 0);
since I forward everyone from cart to checkout (don't show the header in checkout) all the changes made there woun't work in the header except if You refresh the page. It might be like this:

In catalog/view/javascript/common.js find all:

Code: Select all

$('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
add before each:

Code: Select all

$('#cart-total').html(json['cartcount']);
I hope this gives You a clue how to solve this.

Newbie

Posts

Joined
Tue Oct 29, 2013 3:11 am

Who is online

Users browsing this forum: No registered users and 3 guests