feho1 Posted June 12, 2020 Share Posted June 12, 2020 (edited) Hi, following the official guide how to create module i have created my custom module and now i want to call JavaScript function or file to execute function in my module using hooks,in my from custom module from hookActionCustomerAccountUpdate or any other hook i tried but unsuccessful , I have registered the hooks and $this->context->controller->registerJavascript( // 'my-module', // 'modules/' . $this->name . '/views/js/my-module.js', // array( // 'position' => 'bottom', // 'priority' => 2 // ) // ); $this->context->controller->registerJavascript( 'my-module', 'modules/' . $this->name . '/views/js/my-module.js', array( 'position' => 'bottom', 'priority' => 2 ) ); and when i enter this into hookActionCustomerAccountUpdate it does not call the javascript which is alert, but in header it always calls in every page understandably. What I have tried: calling header hook from hookActionCustomerAccountUpdate Hook::exec, with this->hookHeader. My question is how do i call javascript or javascript function or Ajax on hook in module ? Edited June 17, 2020 by feho1 (see edit history) Link to comment Share on other sites More sharing options...
musicmaster Posted June 15, 2020 Share Posted June 15, 2020 Do you have the optimization setting that javascript is moved to the bottom enabled? Often that causes this kind of problems. 1 Link to comment Share on other sites More sharing options...
feho1 Posted June 15, 2020 Author Share Posted June 15, 2020 27 minutes ago, musicmaster said: Do you have the optimization setting that javascript is moved to the bottom enabled? Often that causes this kind of problems. I'm not sure what you mean. In the back office or in module php files? Link to comment Share on other sites More sharing options...
musicmaster Posted June 15, 2020 Share Posted June 15, 2020 backoffice 1 Link to comment Share on other sites More sharing options...
feho1 Posted June 15, 2020 Author Share Posted June 15, 2020 (edited) 3 hours ago, musicmaster said: backoffice Yes found it Smart cache for JavaScript, it is disabled, should i enable ? Or can you show me an example how to call javascript or ajax function from module inside action hook ? Edited June 15, 2020 by feho1 (see edit history) Link to comment Share on other sites More sharing options...
musicmaster Posted June 16, 2020 Share Posted June 16, 2020 No, you shouldn't disable that. Moving javascript to the bottom was an option in PS 1.6. In 1.7 you would need some extension for it. 1 Link to comment Share on other sites More sharing options...
feho1 Posted June 16, 2020 Author Share Posted June 16, 2020 3 minutes ago, musicmaster said: No, you shouldn't disable that. Moving javascript to the bottom was an option in PS 1.6. In 1.7 you would need some extension for it. I dont have the move javascript to the bottom i have only " Smart cache for JavaScript" and enabled it, but still cant call from module a javascript function or Ajax. i have looked other modules but dont get how to call ajax or javascript, I just want to call ajax function after user updated information with hookActionCustomerAccountUpdate hook. Link to comment Share on other sites More sharing options...
EvaF Posted June 16, 2020 Share Posted June 16, 2020 check if hook "actionCustomerAccountUpdate" is correctly registered in your 'my-module' SELECT m.*, hm.*, h.name as hook FROM `ps_module` m JOIN ps_hook_module hm ON hm.id_module = m.id_module JOIN ps_hook h on h.id_hook = hm.id_hook WHERE m.name = 'my-module' 1 Link to comment Share on other sites More sharing options...
feho1 Posted June 17, 2020 Author Share Posted June 17, 2020 15 hours ago, EvaF said: check if hook "actionCustomerAccountUpdate" is correctly registered in your 'my-module' SELECT m.*, hm.*, h.name as hook FROM `ps_module` m JOIN ps_hook_module hm ON hm.id_module = m.id_module JOIN ps_hook h on h.id_hook = hm.id_hook WHERE m.name = 'my-module' Yes it is registered: Link to comment Share on other sites More sharing options...
EvaF Posted June 17, 2020 Share Posted June 17, 2020 ok, I have checked the Hook::exec of ActionCustomerAccountUpdate and it seems to me, that your javascript cannot be placed because of redirect. (controllers/front/IdentityController.php line 55) // within submit is processed save -> // within save is processed update -> // within successfull update is executed hook actionCustomerAccountUpdate if ($customer_form->submit()) { $this->success[] = $this->trans('Information successfully updated.', array(), 'Shop.Notifications.Success'); $should_redirect = true; } else { $this->errors[] = ... } } ... if ($should_redirect) { $this->redirectWithNotifications($this->getCurrentURL()); } 1 Link to comment Share on other sites More sharing options...
feho1 Posted June 17, 2020 Author Share Posted June 17, 2020 36 minutes ago, EvaF said: ok, I have checked the Hook::exec of ActionCustomerAccountUpdate and it seems to me, that your javascript cannot be placed because of redirect. (controllers/front/IdentityController.php line 55) // within submit is processed save -> // within save is processed update -> // within successfull update is executed hook actionCustomerAccountUpdate if ($customer_form->submit()) { $this->success[] = $this->trans('Information successfully updated.', array(), 'Shop.Notifications.Success'); $should_redirect = true; } else { $this->errors[] = ... } } ... if ($should_redirect) { $this->redirectWithNotifications($this->getCurrentURL()); } Thank you for the info, should i override it ? is that possible ? Link to comment Share on other sites More sharing options...
EvaF Posted June 17, 2020 Share Posted June 17, 2020 (edited) I do not know where (at which page - my-account, or identity or..) do you want to load your javascript file. to be true, when I am reading list of your hooks, I would do it by simply way: ( there are certainly more ellegant way) in hookActionCustomerAccountUpdate i would set the cookie value f.e. $this->context->cookie->customer_account_update = true; and in hookHeader I would check if cookie "customer_account_update" is set if (isset($this->context->cookie->customer_account_update)){ $this->context->controller->registerJavascript( 'my-module', 'modules/' . $this->name . '/views/js/my-module.js', array( 'position' => 'bottom', 'priority' => 2 ) ); unset($this->context->cookie->customer_account_update); } P.S. I wrote it blindly - didn't test it Edited June 17, 2020 by EvaF (see edit history) 1 Link to comment Share on other sites More sharing options...
feho1 Posted June 17, 2020 Author Share Posted June 17, 2020 7 hours ago, EvaF said: I do not know where (at which page - my-account, or identity or..) do you want to load your javascript file. to be true, when I am reading list of your hooks, I would do it by simply way: ( there are certainly more ellegant way) in hookActionCustomerAccountUpdate i would set the cookie value f.e. $this->context->cookie->customer_account_update = true; and in hookHeader I would check if cookie "customer_account_update" is set if (isset($this->context->cookie->customer_account_update)){ $this->context->controller->registerJavascript( 'my-module', 'modules/' . $this->name . '/views/js/my-module.js', array( 'position' => 'bottom', 'priority' => 2 ) ); unset($this->context->cookie->customer_account_update); } P.S. I wrote it blindly - didn't test it Thank you very much. your code works flawlessly Marking as solved . Thanks again for your time and sharing your knowledge.. And Thanks to musicmaster too 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