Post by Reptile » Thu Apr 10, 2025 1:36 pm

I am upgrading one of my extensions to Opencart 4x but I am having trouble getting my code injected using the even system.

What I am trying to do is add a few lines of code to the catalog/controller/checkout/register|save method. (Continue button on the checkout page)
I have no trouble registering the after of before event and my custom method is also triggered.

But when checking the $data in my before or after method it is always an empty array.
Is this maybe because the save method returns a JSON response?

I need to add 2 lines at the bottom of the register|save method to add a redirect to the json response.
What am I missing here?

All my extensions: Click here or Click here


User avatar
Active Member

Posts

Joined
Mon Jan 30, 2012 7:23 pm
Location - The Netherlands

Post by OSWorX » Thu Apr 10, 2025 4:49 pm

Reptile wrote:
Thu Apr 10, 2025 1:36 pm
But when checking the $data in my before or after method it is always an empty array.
Is this maybe because the save method returns a JSON response?
What do you mean with the $data ?
Is it that array handed over from the call?
Or what?

Would help much if you could post your code here.

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 Reptile » Thu Apr 10, 2025 5:37 pm

OSWorX wrote:
Thu Apr 10, 2025 4:49 pm
Reptile wrote:
Thu Apr 10, 2025 1:36 pm
But when checking the $data in my before or after method it is always an empty array.
Is this maybe because the save method returns a JSON response?
What do you mean with the $data ?
Is it that array handed over from the call?
Or what?

Would help much if you could post your code here.
Yes that is exactly what I mean.
The second argument.

All my extensions: Click here or Click here


User avatar
Active Member

Posts

Joined
Mon Jan 30, 2012 7:23 pm
Location - The Netherlands

Post by paulfeakins » Thu Apr 10, 2025 6:25 pm

Reptile wrote:
Thu Apr 10, 2025 1:36 pm
I am upgrading one of my extensions to Opencart 4x but I am having trouble getting my code injected using the even system.
Use OCMOD.

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


User avatar
Legendary Member
Online

Posts

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

Post by OSWorX » Thu Apr 10, 2025 9:51 pm

paulfeakins wrote:
Thu Apr 10, 2025 6:25 pm
Reptile wrote:
Thu Apr 10, 2025 1:36 pm
I am upgrading one of my extensions to Opencart 4x but I am having trouble getting my code injected using the even system.
Use OCMOD.
General a bad advice.
I know your agruments Paul, but doing such an easy task with a simple event cost a few minutes only.
And OCMod is here not required.

@Reptile saying it again: post your code here, everything else is "assuming" (and wasting my time).
And write is it a before or after event - that's very important!

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 Reptile » Fri Apr 11, 2025 12:31 am

OSWorX wrote:
Thu Apr 10, 2025 9:51 pm
paulfeakins wrote:
Thu Apr 10, 2025 6:25 pm
Reptile wrote:
Thu Apr 10, 2025 1:36 pm
I am upgrading one of my extensions to Opencart 4x but I am having trouble getting my code injected using the even system.
Use OCMOD.
General a bad advice.
I know your agruments Paul, but doing such an easy task with a simple event cost a few minutes only.
And OCMod is here not required.

@Reptile saying it again: post your code here, everything else is "assuming" (and wasting my time).
And write is it a before or after event - that's very important!
Sorry wasn't at my computer today.
But here is the code:

Code: Select all

public function install() {
		$this->load->model('setting/event');

		$separator = (substr(VERSION, 0, 7) < '4.0.2.0') ? '|' : '.';

		$event = [$this->extension_name, 'catalog/controller/checkout/register|save/before', 'extension/' . $this->extension_name . '/module/' . $this->module_name . $separator . 'custom_extenstion', '', 1, true];
		
		if (VERSION == '4.0.0.0') {
			$this->model_setting_event->addEvent($event[0], $event[3], $event[1], $event[2], $event[5], $event[4]);
		} else {
			$this->model_setting_event->addEvent([
				'code' => $event[0], 
				'trigger' => $event[1],
				'action' => $event[2],
				'description' => $event[3],
				'sort_order' => $event[4],
				'status' => $event[5]
			]);
		}
	}

Code: Select all

<?php
namespace Opencart\Catalog\Controller\Extension\CustomExtension\Module;

class CustomExtension extends \Opencart\System\Engine\Controller {
	public function custom_extension(&$route, &$data) {

		$json['redirect'] = $this->url->link('checkout/checkout');

	}
}
The checkout/register|save method triggers after clicking the Continue button on the checkout page after filling in your address.
When I print_r or echo in the CustomExtension controller I can see this in my network tab when the api call is done.
But I can't seem to inject the $json['redirect'] line of code in the checkout/register controller in the save method.
Also tried the after event.

All my extensions: Click here or Click here


User avatar
Active Member

Posts

Joined
Mon Jan 30, 2012 7:23 pm
Location - The Netherlands

Post by OSWorX » Fri Apr 11, 2025 1:02 am

Well, such makes no sense:

Code: Select all

class CustomExtension extends \Opencart\System\Engine\Controller {
	public function custom_extension(&$route, &$data) {

		$json['redirect'] = $this->url->link('checkout/checkout');

	}
}
because you have "only" the $data array to "manipulate" ..

Such like that would work:

Code: Select all

class CustomExtension extends \Opencart\System\Engine\Controller {
	public function custom_extension(&$route, &$data) {

		$data['json']['redirect'] = $this->url->link('checkout/checkout');

	}
}
Then you can work with the $data['json'] or $json array (depends if $data is used/assigned in/to a template or controller later).

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 Reptile » Fri Apr 11, 2025 1:13 am

OSWorX wrote:
Fri Apr 11, 2025 1:02 am
Well, such makes no sense:

Code: Select all

class CustomExtension extends \Opencart\System\Engine\Controller {
	public function custom_extension(&$route, &$data) {

		$json['redirect'] = $this->url->link('checkout/checkout');

	}
}
because you have "only" the $data array to "manipulate" ..

Such like that would work:

Code: Select all

class CustomExtension extends \Opencart\System\Engine\Controller {
	public function custom_extension(&$route, &$data) {

		$data['json']['redirect'] = $this->url->link('checkout/checkout');

	}
}
Then you can work with the $data['json'] or $json array (depends if $data is used/assigned in/to a template or controller later).
I understand and I have tried this already.
The $data array is always empty.
Even in the after event.

And when using this:

Code: Select all

$data['json']['redirect'] = $this->url->link('checkout/checkout');
I get the following error:
<b>Error</b>
: Unknown named parameter $json in <b>/var/www/oc4000/system/engine/action.php</b>
on line <b>74</b>

And if you look in the checkout/register save method all it does is build up a $json response to output it to the jquery ajax call.

Could it be that there is no $data because the controller is called from the ajax call instead of PHP?

All my extensions: Click here or Click here


User avatar
Active Member

Posts

Joined
Mon Jan 30, 2012 7:23 pm
Location - The Netherlands

Post by Reptile » Fri Apr 11, 2025 4:51 am

Nevermind I finally managed to get it working with the after event:

Code: Select all

public function customExtension(string &$route, array &$data, mixed &$output) {
	// Load the controller as the $data array will always be empty if the controllers is called through an ajax call
	$this->load->controller('checkout/register');

	// Get the json array
	$json = json_decode($this->response->getOutput(),true);

	// Add a value to the json array
	$json['redirect'] = $this->url->link('checkout/checkout');

	// Update the output
	$this->response->setOutput(json_encode($json));
}

All my extensions: Click here or Click here


User avatar
Active Member

Posts

Joined
Mon Jan 30, 2012 7:23 pm
Location - The Netherlands
Who is online

Users browsing this forum: No registered users and 3 guests