alex_u Posted July 14, 2016 Share Posted July 14, 2016 Hi, I was searching for answer how to create new custom action-hook and didn't find how to implement it properly. So the main function of it is when customer visits product the module should save product's id into custom module's table in database and send through API to another database with some additional information. I'm using latest version of PrestaShop. Already added function for it in module hookActionMyModuleCustomerVisitProduct($params) with basic sql-based operations code in it and registered it on install() section if (!$this->installDb() || !$this->installTab() || !parent::install() || !$this->registerHook('displayBackOfficeHeader') || !$this->registerHook('actionMyModuleCustomerVisitProduct')) { return false; } Hook is in database too.. and can I see it on Positions menu. But when I trying to visit some products as a customer nothing happens Maybe I am missing something here.. Link to comment Share on other sites More sharing options...
rocky Posted July 14, 2016 Share Posted July 14, 2016 I think you've forgotten to call your hook. You could do this by overriding ProductController.php and adding code like the following: <?php class ProductController extends ProductControllerCore { public function init() { parent::init(); if (isset($this->context->customer) && (int)$this->context->customer->id > 0) { Hook::exec('actionMyModuleCustomerVisitProduct', array( 'id_customer' => (int)$this->context->customer->id, 'id_product' => (int)Tools::getValue('id_product')) ); } } } This should call your hook and then allow you to use $params['id_customer'] and $params['id_product'] in your function. Link to comment Share on other sites More sharing options...
vekia Posted July 15, 2016 Share Posted July 15, 2016 if customer will not be logged it will spawn some php warning probably. I mean that this will not exist: $this->context->customer->id, so in effect php warn will occur am i right? or if we cast variable as on the example - with (int) - warn will be muted? Link to comment Share on other sites More sharing options...
rocky Posted July 15, 2016 Share Posted July 15, 2016 That's why I was using isset($this->context->customer), but you're right that I forgot a cast, so I updated my post above. 1 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