sinai Posted December 20, 2017 Share Posted December 20, 2017 Hi all, I want to know how can I execute some codes after a product is created/updated in prestashop. I have done such things for wordpress via the functions.php file inside a theme. I want to know whether there is a similar way in prestashop or not? I want to purge the cache server after a product is updated/created. Thanks in advanced. Link to comment Share on other sites More sharing options...
tarek.fellah Posted December 20, 2017 Share Posted December 20, 2017 You can use action hooks, actionProductAdd and actionProductUpdate You need this hooks on the module install and add function for each hook Hook::exec are called on AdminProductController.php Hook::exec('actionProductAdd', array('id_product' => (int)$this->object->id, 'product' => $this->object)); public function HookActionProductUpdate($params){ Var_dump($params['product']);exit; } Cordially. Link to comment Share on other sites More sharing options...
sinai Posted December 20, 2017 Author Share Posted December 20, 2017 (edited) 2 hours ago, tarek.fellah said: You can use action hooks, actionProductAdd and actionProductUpdate You need this hooks on the module install and add function for each hook Hook::exec are called on AdminProductController.php Hook::exec('actionProductAdd', array('id_product' => (int)$this->object->id, 'product' => $this->object)); public function HookActionProductUpdate($params){ Var_dump($params['product']);exit; } Cordially. Thank you tarek.fellah. I found the AdminProductsController.php which is located in the path /home/username/public_html/controllers/admin. I want to know whether this file is updated or not if someone updates its prestashop or change its theme? Because if it is updated whenever presta release a new version or by changing a theme, all my codes will disappear! Thank you. Edited December 20, 2017 by sinai (see edit history) Link to comment Share on other sites More sharing options...
tarek.fellah Posted December 20, 2017 Share Posted December 20, 2017 You can do like this on your module public function HookActionProductUpdate($params){ Var_dump($params['product']);exit; } This is what i do and it's working. I don't think Prestashop will change hook names. Link to comment Share on other sites More sharing options...
sinai Posted December 20, 2017 Author Share Posted December 20, 2017 4 hours ago, tarek.fellah said: You can do like this on your module public function HookActionProductUpdate($params){ Var_dump($params['product']);exit; } This is what i do and it's working. I don't think Prestashop will change hook names. I am a little confused! because I am not an expert PHP developer. I just want to execute some simple php codes. May you please tell me where should I add the function and the hook? I added a function at the end of the AdminProductsController.php file, and call it just after the line Hook::exec('actionProductAdd', array('id_product' => (int)$this->object->id, 'product' => $this->object)); in function processAdd() the function is as follows: public function update_test() { file_put_contents("/home/username/public_html/controllers/admin/test.txt","OK"); return ; } but I get the following error: Got error 'PHP message: PHP Fatal error: Call to undefined function update_test() in /home/smilehyper/public_html/controllers/admin/AdminProductsController.php on line 1889 Link to comment Share on other sites More sharing options...
tarek.fellah Posted December 21, 2017 Share Posted December 21, 2017 You should learn to create a module, i think you need only a simple module http://doc.prestashop.com/display/PS16/Creating+a+first+module Link to comment Share on other sites More sharing options...
sinai Posted December 22, 2017 Author Share Posted December 22, 2017 You are right. I studied a little about creating a simple module and then created a module and registered my desired action hooks. I am developing it now. Thank you. Link to comment Share on other sites More sharing options...
veijari Posted May 12, 2020 Share Posted May 12, 2020 @sinai Does this hookactionProductAdd work in PS 1.7x? I've tried to use it but it doesn't seem to fire when adding a product in backoffice. Link to comment Share on other sites More sharing options...
androkot33 Posted September 7, 2020 Share Posted September 7, 2020 @veijari Hook actionProductAdd does exist in PrestaShop 1.7 - https://devdocs.prestashop.com/1.7/modules/concepts/hooks/list-of-hooks/ 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