Post by Lumirion » Thu Oct 03, 2024 11:32 pm

Using Opencart 3.0.3.3.
How do I access data, created by one controller, in a second controller? For instance, one controller generates the following object and makes it available to the view.

Code: Select all

$data['products'][] = array(
                'product_id' => $result['product_id'],
                'thumb' => $image,
                'name' => $result['name'],
                'description' => utf8_substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
                'price' => $price,
                'special' => $special,
                'tax' => $tax,
                'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
                'rating' => $result['rating'],
                'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
            );
Now, rather than duplicating all the work done by the first controller, I want the second controller to access the same {{ ‘products’ }} that the twig file has access to. Is there some this->Data->get(‘products’); function I can use in the second controller?

New member

Posts

Joined
Fri Apr 19, 2024 1:32 am

Post by OSWorX » Thu Oct 03, 2024 11:35 pm

You could achieve such with an event.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by Lumirion » Fri Oct 04, 2024 6:14 am

How would I get the event to dispatch between the 2 controllers? Can the event access the stored variables the twig file references?

New member

Posts

Joined
Fri Apr 19, 2024 1:32 am

Post by OSWorX » Fri Oct 04, 2024 1:58 pm

Lumirion wrote:
Fri Oct 04, 2024 6:14 am
How would I get the event to dispatch between the 2 controllers? Can the event access the stored variables the twig file references?
It depends what you want to do ..

Either call the event before the controller/view is called, or after.
Depending on that, you have access to several variables and can "manipulate" (add/remove/change) whatever you want/need.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by paulfeakins » Mon Oct 07, 2024 6:39 pm

OSWorX wrote:
Thu Oct 03, 2024 11:35 pm
You could achieve such with an event.
Alright straightlight.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by paulfeakins » Mon Oct 07, 2024 6:40 pm

Lumirion wrote:
Thu Oct 03, 2024 11:32 pm
How do I access data, created by one controller, in a second controller?
You shouldn't. Data should come from models and both controllers should call the same method in a model.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by nonnedelectari » Tue Oct 08, 2024 12:44 am

Lumirion wrote:
Thu Oct 03, 2024 11:32 pm
Using Opencart 3.0.3.3.
How do I access data, created by one controller, in a second controller? For instance, one controller generates the following object and makes it available to the view.

Code: Select all

$data['products'][] = array(
                'product_id' => $result['product_id'],
                'thumb' => $image,
                'name' => $result['name'],
                'description' => utf8_substr(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
                'price' => $price,
                'special' => $special,
                'tax' => $tax,
                'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
                'rating' => $result['rating'],
                'href' => $this->url->link('product/product', 'product_id=' . $result['product_id']),
            );
Now, rather than duplicating all the work done by the first controller, I want the second controller to access the same {{ ‘products’ }} that the twig file has access to. Is there some this->Data->get(‘products’); function I can use in the second controller?
Apart from events,
You can also just pass the products array as a parameter to the target controller.

I use that to pass settings to a product listng module:

Code: Select all

$module_name = 'products';
$module_setting = array(
	'csrf_token'		=> $csrf_token,
	'view' 		=> $module_view,
	'product_kind'	=> $product_kind,
	'limit' 		=> 8, 
	'type' 		=> 'PATH',
	'sort' 		=> 'RAND',
	'category' 		=> $product_category_id,
	'filter_sub_category' 	=> true,
	'exclude' 		=> $product_id,
	'only_not_sold' 	=> true,
	'only_stock' 	=> true
	);

$output = $this->load->controller('extension/module/' . $module_name, $module_setting);
Then in the target controller, extension/module/products.php in my case, you give the index function the parameter:

Code: Select all

public function index($setting) {
	........
}
So you can pass your products array in the same manner.

Active Member

Posts

Joined
Thu Mar 04, 2021 6:34 pm
Who is online

Users browsing this forum: Bing [Bot] and 24 guests