AminaM Posted September 2, 2022 Share Posted September 2, 2022 (edited) Hi, I am trying to define a global variable that will be used in a custom module, but the module's tpl file always displays a null value. I am guessing it is a problem with the variable scope, so I am trying to create this new global variable wherever prestashop's global variables are defined. This has taken a lot of my time to figure out, but I still haven't got anything. Does someone know how to proceed? Edited September 5, 2022 by AminaM (see edit history) Link to comment Share on other sites More sharing options...
Rhobur Posted September 3, 2022 Share Posted September 3, 2022 Use the hook displayHeader in your main class to add a variable accessible from every shop page. Link to comment Share on other sites More sharing options...
AminaM Posted September 4, 2022 Author Share Posted September 4, 2022 Thank you for your answer, but I am not sure to understand... The hook displayHeader should be called from a FrontController from what I understand, but whenever I define a variable in my FrontController and try to display it in the module's tpl file, I am faced with the same problem. Link to comment Share on other sites More sharing options...
webprog Posted September 4, 2022 Share Posted September 4, 2022 21 minutes ago, AminaM said: Thank you for your answer, but I am not sure to understand... The hook displayHeader should be called from a FrontController from what I understand, but whenever I define a variable in my FrontController and try to display it in the module's tpl file, I am faced with the same problem. Use the install function in your module to register hook like this: public function install() { if (!parent::install() || !$this->registerHook('displayHeader')) { return false; } } And the content of the hook will be in this function: public function hookdisplayHeader() { code content } Link to comment Share on other sites More sharing options...
AminaM Posted September 4, 2022 Author Share Posted September 4, 2022 (edited) I will try your suggestiong, thanks! However, I am wondering if I can override the module's main class instead of directly adding it to the module itself, since it was bought from a third party. I am not sure how to proceed to get the hook installed while using the override principle. Basically, I have a module for additionnal registration fields. Inside this module is the main php file and a template. Everything works great when I set my smarty variable directly in the main class, but it doesn't work anymore when I override it and try to get my custom changes outside of the module. That's my big issue at the moment...and I would be really grateful if someone could give me a solution for that! Edited September 5, 2022 by AminaM (see edit history) Link to comment Share on other sites More sharing options...
Rhobur Posted September 5, 2022 Share Posted September 5, 2022 Yes you can modify that module's main class and add this hook. Reset the module afterwards and all will be OK. Link to comment Share on other sites More sharing options...
AminaM Posted September 5, 2022 Author Share Posted September 5, 2022 Awesome! And do you think there will be issues in the future if changes are made by the module's owner? What will happen to my new hook? Link to comment Share on other sites More sharing options...
Rhobur Posted September 5, 2022 Share Posted September 5, 2022 Of course, if you will install another version coming from the module's developer your changes will be lost and you will have to re apply them. Link to comment Share on other sites More sharing options...
AminaM Posted September 5, 2022 Author Share Posted September 5, 2022 And apart from that solution, could there be another one? Like setting the variable in a controller/class where it would be accessible by any module? I have tried putting it in most front controllers, without success. If there is no other solution, I will stick to the one you suggested, since it does what I want it to do. But I have been told to avoid modifying the module's main class and to find another solution, so if there is one I haven't thought of that you would apply in a similar situation, I would love to hear it. Again, thank you so much for your help and your time! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now