Post by geonik » Wed Jan 25, 2023 1:59 am

Hello,
I'm new in the programming world and to opencart and I would like to know if there is any way to access the data of a variable that is placed in a file, from another file that could even be in another directory.

For example I am in the catalog/model/extension/payment/free_checkout.php file,

and I want to access a variable that is in catalog/model/journal3/checkout.php

What I'm trying to do is enabling/disabling free checkout on checkout page (by making it's status true or false), for orders that have a certain shipping method.
So I want have access to the shipping method, which I have saved in a variable in the second file in this example.

Sorry if the question is too newbie, and thank you for your time.
Last edited by geonik on Fri Jan 27, 2023 7:41 pm, edited 1 time in total.

Newbie

Posts

Joined
Wed Jan 25, 2023 1:43 am

Post by ADD Creative » Wed Jan 25, 2023 6:47 pm

Assuming 3.0.x, have you tried? Where x is the code for the shipping method you want to check.

Code: Select all

if (isset($this->session->data['shipping_method']['code']) && $this->session->data['shipping_method']['code'] == 'x') {
	// do something
}

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by geonik » Thu Jan 26, 2023 5:56 pm

ADD Creative wrote:
Wed Jan 25, 2023 6:47 pm
Assuming 3.0.x, have you tried? Where x is the code for the shipping method you want to check.

Code: Select all

if (isset($this->session->data['shipping_method']['code']) && $this->session->data['shipping_method']['code'] == 'x') {
	// do something
}
Thanks for your reply,
I'm running opencart 3.0.3.8.
The error in my case was that I was using data['shipping_code'].
It works now!

So the way to access a variable that I made in a file, is to store it inside the $data array ?

Newbie

Posts

Joined
Wed Jan 25, 2023 1:43 am

Post by ADD Creative » Thu Jan 26, 2023 8:19 pm

The files you mention will be two different requests, so you will need to store the variable in the session.

Code: Select all

$this->session->data['my_var'] = 'something';
Be aware that it will stay for the life of the session unless unset. You may be best just to check the shipping code set in the session in the free_checkout.php.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by geonik » Fri Jan 27, 2023 1:12 am

ADD Creative wrote:
Thu Jan 26, 2023 8:19 pm

Be aware that it will stay for the life of the session unless unset. You may be best just to check the shipping code set in the session in the free_checkout.php.
Oh I see..
Is there a way to control how long the session will last?

Newbie

Posts

Joined
Wed Jan 25, 2023 1:43 am

Post by ADD Creative » Fri Jan 27, 2023 7:12 pm

geonik wrote:
Fri Jan 27, 2023 1:12 am
Oh I see..
Is there a way to control how long the session will last?
Sessions do expire and you can adjust the time, but I don't see how that would help you.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by geonik » Fri Jan 27, 2023 7:40 pm

ADD Creative wrote:
Fri Jan 27, 2023 7:12 pm
geonik wrote:
Fri Jan 27, 2023 1:12 am
Oh I see..
Is there a way to control how long the session will last?
Sessions do expire and you can adjust the time, but I don't see how that would help you.
It's for educational purposes and testing only..!

Newbie

Posts

Joined
Wed Jan 25, 2023 1:43 am

Post by ADD Creative » Fri Jan 27, 2023 8:53 pm

Session lifetime is controlled by a combination of the following. However, OpenCart 3.0.x work slightly differently as it has its own session handler, which you would have to fix the bugs in. You also have to remember every request will reset the session lifetime.

https://www.php.net/manual/en/session.c ... e-lifetime
https://www.php.net/manual/en/session.c ... axlifetime

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom
Who is online

Users browsing this forum: No registered users and 46 guests