Post by veganista » Wed May 12, 2010 2:48 am

Hello Folks,

Im developing a site for a client and its the first time i've tried to work with opencart so im not 100% sure about the best way to do things, i could do with some advice.

I have added the following code to the header controller in controllers/common/header.php

Code: Select all

        if (isset($this->session->data['cart']) && !empty($this->session->data['cart'])) {
            $this->data['cart_count'] = array_sum($this->session->data['cart']);
        }else{
            $this->data['cart_count'] = 0;
        }
 
So this has the affect of lettign me use $cart_count in my header template.

Is the correct way to go about modifying opencart? It doesn't seem like its very upgrade proof.

Thanks

New member

Posts

Joined
Mon Apr 26, 2010 11:50 pm

Post by johnnyart » Wed May 12, 2010 5:50 am

There is already a written function in the cart library (system/library) that returns that value.

You can store it on your own variable, remember that to access it from the view file (.tpl) you need to type directly the variable name, in this case 'yourvar' instead of the data array:

Code: Select all

$this->data['yourVar'] = $this->cart->countProducts(); 
then you can print the value like this:

Code: Select all

echo $yourVar; 
Of course you already knew that part, but I cant be bothered deleting it :)
Hope it helps
Last edited by johnnyart on Wed May 12, 2010 4:57 pm, edited 1 time in total.

User avatar
Newbie

Posts

Joined
Tue Mar 02, 2010 5:13 pm

Post by Qphoria » Wed May 12, 2010 7:49 am

Actually you're both right.. partially

Instead of $data you do need $this->data

And you don't really need an if/else because the $this->cart->countProducts(); will return a valid response either way
So simply

Code: Select all

$this->data['cart_count'] = $this->cart->countProducts(); 

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by johnnyart » Wed May 12, 2010 4:56 pm

Oh sorry, It was a typo. Corrected now.
Thanks Q

User avatar
Newbie

Posts

Joined
Tue Mar 02, 2010 5:13 pm
Who is online

Users browsing this forum: No registered users and 9 guests