Page 3 of 3
Re: OC 3.0.x Events System: How to modify twig template file?
Posted: Tue Jan 09, 2024 5:08 am
by samdz
JNeuhoff wrote: ↑Tue Jan 09, 2024 2:33 am
It's a similar logic for OpenCart 4:
Code: Select all
// event handler for admin/view/catalog/product_form/before
public function eventViewCatalogProductFormBefore( string &$route, array &$data, string &$template_code ): void {
// ....
$code = $this->getTemplateCode($route,$code);
// .... modify $code
$template_code = $code;
}
protected function getTemplateCode( $route, &$code ) {
if (!empty($code)) {
return $code;
}
$file = DIR_TEMPLATE . $route . '.twig';
if (is_file($file)) {
$code = file_get_contents( $file );
} else {
trigger_error("Cannot find template file for route '$route'");
exit;
}
return $code;
}
Thank you for your reply,
I have tested the code but I got error:
Code: Select all
Notice: Cannot find template file for route 'extension/opencart/dashboard/recent_info'
Note I want to edit the this file:
Code: Select all
admin/view/extension/opencart/dashboard/recent_info.twig
Any help!
Re: OC 3.0.x Events System: How to modify twig template file?
Posted: Tue Jan 09, 2024 6:20 pm
by JNeuhoff
What's in your DIR_TEMPLATE?
Re: OC 3.0.x Events System: How to modify twig template file?
Posted: Tue Jan 09, 2024 10:42 pm
by samdz
JNeuhoff wrote: ↑Tue Jan 09, 2024 6:20 pm
What's in your DIR_TEMPLATE?
I am using on localhost (wampserver);
it's on config file like this:
Code: Select all
define('DIR_TEMPLATE', DIR_APPLICATION . 'view/template/');
I am tring to add CUSTOMER ID column to recent module (latest orders) in the dashboard page.
Re: OC 3.0.x Events System: How to modify twig template file?
Posted: Tue Jan 09, 2024 11:05 pm
by JNeuhoff
So what's in your DIR_APPLICATION then?
Re: OC 3.0.x Events System: How to modify twig template file?
Posted: Wed Jan 10, 2024 12:25 am
by samdz
JNeuhoff wrote: ↑Tue Jan 09, 2024 11:05 pm
So what's in your DIR_APPLICATION then?
this is my admin config file:
Code: Select all
<?php
// APPLICATION
define('APPLICATION', 'Admin');
// HTTP
define('HTTP_SERVER', 'http://localhost/oc4024e/admin21/');
define('HTTP_CATALOG', 'http://localhost/oc4024e/');
// DIR
define('DIR_OPENCART', 'C:/wamp64/www/oc4024e/');
define('DIR_APPLICATION', DIR_OPENCART . 'admin21/');
define('DIR_EXTENSION', DIR_OPENCART . 'extension/');
define('DIR_IMAGE', DIR_OPENCART . 'image/');
define('DIR_SYSTEM', DIR_OPENCART . 'system/');
define('DIR_CATALOG', DIR_OPENCART . 'catalog/');
define('DIR_STORAGE', 'C:/wamp64/storage4024e/');
define('DIR_LANGUAGE', DIR_APPLICATION . 'language/');
define('DIR_TEMPLATE', DIR_APPLICATION . 'view/template/');
define('DIR_CONFIG', DIR_SYSTEM . 'config/');
define('DIR_CACHE', DIR_STORAGE . 'cache/');
define('DIR_DOWNLOAD', DIR_STORAGE . 'download/');
define('DIR_LOGS', DIR_STORAGE . 'logs/');
define('DIR_SESSION', DIR_STORAGE . 'session/');
define('DIR_UPLOAD', DIR_STORAGE . 'upload/');
// DB
define('DB_DRIVER', 'mysqli');
define('DB_HOSTNAME', 'localhost');
define('DB_USERNAME', 'oc4024e');
define('DB_PASSWORD', '123456');
define('DB_SSL_KEY', '');
define('DB_SSL_CERT', '');
define('DB_SSL_CA', '');
define('DB_DATABASE', 'oc4024e');
define('DB_PORT', '3306');
define('DB_PREFIX', 'oc_');
// OpenCart API
define('OPENCART_SERVER', 'https://www.opencart.com/');
Re: OC 3.0.x Events System: How to modify twig template file?
Posted: Wed Jan 10, 2024 1:20 am
by JNeuhoff
So does the file 'C:/wamp64/www/oc4024e/admin21/view/template/extension/opencart/dashboard/recent_info.twig' exist?
Perhaps the code should be:
Code: Select all
protected function getTemplateCode( $route, &$code ) {
if (!empty($code)) {
return $code;
}
$file = DIR_EXTENSION.'opencart/admin/view/template/dashboard/recent_info.twig';
if (is_file($file)) {
$code = file_get_contents( $file );
} else {
trigger_error("Cannot find template file for route '$route'");
exit;
}
return $code;
}
Your $route looks strange.
Re: OC 3.0.x Events System: How to modify twig template file?
Posted: Thu Jan 11, 2024 5:18 am
by samdz
JNeuhoff wrote: ↑Wed Jan 10, 2024 1:20 am
So does the file 'C:/wamp64/www/oc4024e/admin21/view/template/extension/opencart/dashboard/recent_info.twig' exist?
Perhaps the code should be:
Code: Select all
protected function getTemplateCode( $route, &$code ) {
if (!empty($code)) {
return $code;
}
$file = DIR_EXTENSION.'opencart/admin/view/template/dashboard/recent_info.twig';
if (is_file($file)) {
$code = file_get_contents( $file );
} else {
trigger_error("Cannot find template file for route '$route'");
exit;
}
return $code;
}
Your $route looks strange.
It's working now, thank you so much for your help.
But why it didn't working when use your code and working without your code ??
Re: OC 3.0.x Events System: How to modify twig template file?
Posted: Thu Jan 11, 2024 6:15 pm
by JNeuhoff
It's all about the way how the $route is translated into the correct path of the corresponding twig file. OpenCart has a weird way of dealing with extensions

Re: OC 3.0.x Events System: How to modify twig template file?
Posted: Fri Apr 25, 2025 6:20 pm
by grgr
I've updated the above slightly for 4.1.x, though I have yet to get to the correct path for a theme folder as it seems the theme folder name can be different from the config name for themes, where modules can't.
Code: Select all
protected function getTemplateCode($route, &$code) {
if (!empty($code)) {
return $code;
}
$template_file = DIR_TEMPLATE . $route . '.twig';
if (str_contains($template_file, 'extension/ocmod')) {
$route = str_replace('extension/ocmod/', '', $route);
if (defined('DIR_CATALOG')) {
$template_file = DIR_EXTENSION . 'ocmod/admin/view/template/' . $route . '.twig';
} else {
$template_file = DIR_EXTENSION . 'ocmod/catalog/view/template/' . $route . '.twig';
}
}
if (file_exists($template_file) && is_file($template_file)) {
return file_get_contents($template_file);
} else {
trigger_error("Cannot find template file for route '$route'");
exit;
}
}