Find count of products and total price for added in cart

This is where you share YOUR scripts with others

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 35
Joined: Thu Jun 28, 2012 2:11 pm

Tue Sep 03, 2013 11:25 pm

The solution is for opencart.
Add session variable in catalog/controller/checkout/cart.php like this:

Find this line:
$this->data['products'] = array();

Add this code block
//count no of products added to cart
$_SESSION['count_2'] = count($this->cart->getProducts());

Thanks find this code block:
if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
$total = $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')) * $product['quantity']);
} else {
$total = false;

Add this line:
$_SESSION['total_2'] = $total;

Now find this code block:
} else {

Add this code block:
$_SESSION['count_2'] = "";
$_SESSION['total_2'] = "";

Here you can see we are using the opencart function and store values in session.
Now go to your page where you want to display these values and add this code block:

For count of product added in cart:
<?php if(isset($_SESSION['count_2']) && $_SESSION['count_2'] != ""){
echo $_SESSION['count_2'];
} else { echo '0';} ?>

For total price of products added in cart:
<?php if(isset($_SESSION['total_2']) && $_SESSION['total_2'] != ""){
echo $_SESSION['total_2'];
} else { echo '$0.00';} ?>

Problem solved. :)

Post Reply