Code: Select all
if (isset($this->request->get['route']) && $this->request->get['route'] == 'product/product') {
}
Code: Select all
if (isset($this->request->get['route']) && $this->request->get['route'] == 'product/product') {
}
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.Lumirion wrote: ↑Fri Jul 19, 2024 5:15 amPreviously, 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 useto 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?Code: Select all
if (isset($this->request->get['route']) && $this->request->get['route'] == 'product/product') { }
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');
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 Pro • Drag & Drop Sort Order
Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.
Code: Select all
$this->model_setting_event->addEvent('headerinsertstyle', 'catalog/view/common/header/before', 'extension/module/pagefixes/insertpagestyle');
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
}
}
Code: Select all
$code = str_replace("</head>", "<style>"."\r\n\t\t\t".'/* MyStyle */'."\r\n </style></head>", $code);
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 Pro • Drag & Drop Sort Order
Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.
Users browsing this forum: No registered users and 12 guests