Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session Variables in Opencart

Tags:

php

opencart

Can someone explain where session variables are held?

I have added some session variables in header.php in the controller, for example:

$this->session->data['day']=date("d",strtotime($row['startdate']));

This works when loading the site, and when I click on a product, all the variables are gone, except for the [language], [currency] and [cart] which are set by Opencart.

I guess there is another file or controller file where I set the variables, or where [language], [currency] and [cart] are set but I cannot find it.

Thanks in advance.

like image 876
user984689 Avatar asked Dec 12 '11 09:12

user984689


2 Answers

Session values are not set in a file. If you want to set a session variable, use

$this->session->data['variable_name_here'] = 'data value here';

and to retrieve the value you just access

$this->session->data['variable_name_here']

For example, to echo it use

echo $this->session->data['variable_name_here'];
like image 172
Jay Gilford Avatar answered Oct 21 '22 05:10

Jay Gilford


Here I would save the variables into a session:

public function receive() {
    $this->session->data['guest_name'] = $this->request->post['name'];
    $this->session->data['guest_address'] = $this->request->post['address'];
}

Now in catalog/controller/checkout/guest.php at index method check for that session variables and if set, store the value in the $this->data array for presenting to the template:

if(isset($this->session->data['guest_name'])) { // it is enough to check only for one variable and only if it is set
    $this->data['guest_name'] = $this->session->data['guest_name'];
    $this->data['guest_address'] = $this->session->data['guest_address'];
}

After that You can simply echo these values in Your template (still checking whether exists):

<?php if(isset($guest_name)) { ?>
<div><?php echo $guest_name . ' - ' . $guest_address; ?></div>
<?php } ?>

Now You should be done while avoiding any undefined variable notices...

like image 21
Ravi Patel Avatar answered Oct 21 '22 03:10

Ravi Patel