Post by Lumirion » Fri Jul 19, 2024 5:15 am

Previously, I made an extension that adds custom code to the header via an after event. Now I want to do the same but with a before event. For this one I am using Opencart 3.0.3.3. Now, when I did this with the after event, I was able to use

Code: Select all

if (isset($this->request->get['route']) && $this->request->get['route'] == 'product/product') {
}
to wrap my code so it only runs when generating product pages. However, with a before event, this is not working. My code never runs. Is there a different way to tell if the header is about to be used on a product page?
Last edited by Lumirion on Tue Jul 23, 2024 10:16 pm, edited 1 time in total.

New member

Posts

Joined
Fri Apr 19, 2024 1:32 am

Post by softmonke » Fri Jul 19, 2024 2:28 pm

Lumirion wrote:
Fri Jul 19, 2024 5:15 am
Previously, I made an extension that adds custom code to the header via an after event. Now I want to do the same but with a before event. For this one I am using Opencart 3.0.3.3. Now, when I did this with the after event, I was able to use

Code: Select all

if (isset($this->request->get['route']) && $this->request->get['route'] == 'product/product') {
}
to wrap my code so it only runs when generating product pages. However, with a before event, this is not working. My code never runs. Is there a different way to tell if the header is about to be used on a product page?
What are you setting for your before event's trigger? Is it for controller or view? If you are using controller for the trigger, you should try setting the trigger to "common/header" instead. Personally, I feel that the use-cases for controller events are quite limited so the events that I write are mostly using model and view triggers.

Also, when you say that your "code never runs", are you saying that the event was never triggered? If it did not trigger at all, then there is probably something wrong with your event action and/or event trigger. If it is triggered, then the issue probably lies with your code in the event function.

Anyway, I tested both before and after events on the "common/header" view trigger in OpenCart 3.0.3.8, and the code to check the route and whether it is "product/product" works fine. So it's probably something wrong with your setup - do check the following:
  1. Your event is listed in Extensions » Events
  2. Your event is not disabled in Extensions » Events
  3. Your event should be triggering for view and not model or controller
For reference, the following are my code:

For adding the events:

Code: Select all

$this->load->model('setting/event');

$this->model_setting_event->addEvent('module_custom_module', 'catalog/view/common/header/before', 'extension/module/custom_module/eventBeforeCatalogViewCommonHeader');
$this->model_setting_event->addEvent('module_custom_module', 'catalog/view/common/header/after', 'extension/module/custom_module/eventAfterCatalogViewCommonHeader');
My event functions:

Code: Select all

public function eventBeforeCatalogViewCommonHeader($route, &$data) {
    $this->log->write("Before Common Header Event :: triggered");
    
    if (isset($this->request->get['route']) && $this->request->get['route'] == 'product/product') {
        $this->log->write("Before Common Header Event :: product/product route triggered");
    }
}

public function eventAfterCatalogViewCommonHeader($route, &$data, &$output) {
    $this->log->write("After Common Header Event :: triggered");
    
    if (isset($this->request->get['route']) && $this->request->get['route'] == 'product/product') {
        $this->log->write("After Common Header Event :: product/product route triggered");
    }
}

Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager ProDrag & Drop Sort Order

Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by Lumirion » Fri Jul 19, 2024 10:46 pm

Hello @softmonke. I am triggering on the view not the controller.
1. My event is registered and is displayed in Extensions>>Events. I believe the event is triggering but I don’t think my code is running in the event handler. I added a debug line that generates a text file containing the contents of header.twig with my modifications, in the directory of the controller hosting my event. That file is never created, and my modifications never show up in the resultant header.
2. The event is listed as “enabled” in Extensions>>Events.
3. My event only triggers on view.
I registered my event like this:

Code: Select all

$this->model_setting_event->addEvent('headerinsertstyle', 'catalog/view/common/header/before', 'extension/module/pagefixes/insertpagestyle');
I’ll add in some more logging as per the reference code you provided, and check back here in a bit. I’m pretty sure my code is not running. Either that or the output of my event is being overwritten somehow by the default.

New member

Posts

Joined
Fri Apr 19, 2024 1:32 am

Post by Lumirion » Sat Jul 20, 2024 12:18 am

Interesting. I added the event logging code from your example and reinstalled my extension. I refreshed mods cache and opened a product page. Then checked the log. The log output was:
2024-07-19 10:44:57 - Before Common Header Event :: triggered
2024-07-19 10:44:57 - Before Common Header Event :: product/product route triggered

My event handler looks like this:

Code: Select all

public function insertpagestyle(&$route, &$data, &$code = null)
	{
		$this->log->write("Before Common Header Event :: triggered");
		/* Get the route of the current page this header is being used on and see if it is a product page. */
		if (isset($this->request->get['route']) && $this->request->get['route'] == 'product/product') 
		{
			$this->log->write("Before Common Header Event :: product/product route triggered");
			$TwigSource = "";
			$FilePath = "";
			if (!empty($code)) 
			{
				$TwigSource = $code; // If we were passed a header use it.
			}
			else{   // We need the contents of header.twig so load it
				$FilePath = DIR_TEMPLATE . '/MyTheme/template/common/header.twig';
				if (!is_file($FilePath)) 
				{
					$this->log->write("Cannot find template file for route " . $FilePath);
				}else {
					$TwigSource = file_get_contents($FilePath);
				}
			}
			$MyCustomCode = "<style>"."\r\n\t\t\t".'/* MyStyle */'."\r\n </style>";
			$Phrase = '</head>';
			$InsertAt = strpos($TwigSource, $Phrase);   // Find the position of the first instance of $Phrase
			$code = substr($TwigSource, 0, $InsertAt) . $MyCustomCode . substr($TwigSource, $InsertAt); // Get a substring in $TwigSource from start to $InsertAt.  Append $MyCustomCode to it and add the substring in $TwigSource from $InsertAt to the end.
			
			file_put_contents("TwigHeaderCheck.txt", $code,);  // Uncomment for debug output of entire string containing  header.twig with my style
			
		}
	}


New member

Posts

Joined
Fri Apr 19, 2024 1:32 am

Post by softmonke » Sat Jul 20, 2024 4:54 am

Looks like you're just trying to add in your custom style into the header if it's the product route? In this case, why don't you just use the after event, in which case the "&$code" variable will contain the actual output of the header and you can just do a simple str_replace to replace the "</head>" tag with your custom style like the following:

Code: Select all

$code = str_replace("</head>", "<style>"."\r\n\t\t\t".'/* MyStyle */'."\r\n </style></head>", $code);
Anyhow, for a before event triggering on view, I believe the "$code" variable will always be empty. And I tried your code on my own set up and it works fine:

Code: Select all

public function eventBeforeCatalogViewCommonHeader($route, &$data, &$code) {
    $FilePath = DIR_TEMPLATE . '/default/template/common/header.twig';
    if (!is_file($FilePath)) {
      $this->log->write("Cannot find template file for route " . $FilePath);
    } else {
      $TwigSource = file_get_contents($FilePath);
    }
    $MyCustomCode = "<style>"."\r\n\t\t\t".'/* MyStyle */'."\r\n </style>";
    $Phrase = '</head>';
    $InsertAt = strpos($TwigSource, $Phrase);
    $code = substr($TwigSource, 0, $InsertAt) . $MyCustomCode . substr($TwigSource, $InsertAt);
}

Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager ProDrag & Drop Sort Order

Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by Lumirion » Tue Jul 23, 2024 10:15 pm

Well, at least I know my code works. That narrows it down. Must be some other conflicting extension or hackery going on and overriding my override. Time to reverse engineer an Opencart implementation. Thank you guys so much for all the help. I appreciate it.

New member

Posts

Joined
Fri Apr 19, 2024 1:32 am
Who is online

Users browsing this forum: Semrush [Bot] and 17 guests