Peter_17 Posted June 17 Share Posted June 17 Hi, when those hooks are triggerd? Prestashop 8.1.6. actionAdminDeleteAfter actionAdminDeleteBefore actionAdminProductsControllerDeleteAfter actionAdminProductsControllerDeleteBefore I guess that when I delete a product in BO. /adminstrator/index.php/sell/catalog/products-v2 I can install a module with any of this hook but function connected with any of this hooks is not executed when I delete a product. Why? In the same module I successfully used a hook actionAdminDuplicateAfter How else should I execute a function after product deletion? A test module for the hook: At least it should show an error because $PCDA is not defined. <?php declare(strict_types=1); if (!defined('_PS_VERSION_')) { exit; } class DhPcda extends Module { public function __construct() { $this->name = 'dhpcda'; $this->tab = 'administration'; $this->version = '1.0.0'; $this->author = 'aaa.pl'; $this->need_instance = 0; $this->ps_versions_compliancy = [ 'min' => '1.8.0.0', 'max' => _PS_VERSION_, ]; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->trans('AAA PCDA', [], 'Modules.Dhtestduplicate.Dhtestduplicate'); $this->description = $this->trans('Test: ActionAdminProductsControllerDeleteAfter', [], 'Modules.Dhtestduplicate.dhtestduplicate'); } public function install() { if (Shop::isFeatureActive()) { Shop::setContext(Shop::CONTEXT_ALL); } return parent::install() && $this->registerHook('actionAdminProductsControllerDeleteAfter'); } public function uninstall() { return parent::uninstall(); } public function isUsingNewTranslationSystem() { return true; } public function hookActionAdminProductsControllerDeleteAfter($params) { $aaa = $PCDA; file_put_contents("../modules/pcda/dev_logs/array_params_PCDA.json",json_encode($params)); $db = \Db::getInstance(); } } Link to comment Share on other sites More sharing options...
AddWeb Solution Posted June 20 Share Posted June 20 Hi, Here are few Debugging tips: Add a log entry at the beginning of the hook method to check if the method is called at all. Make sure the file path where you are trying to write the logs is correct and writable. Navigate to the product deletion page and delete a product. Then check the logs to see if the hooks are being triggered. Thanks! 1 Link to comment Share on other sites More sharing options...
WisQQ Posted June 20 Share Posted June 20 You could use object hook, it works like this action<yourobjectname>DeleteAfter. In your case this will be actionProductDeleteAfter. This hook will be called after product is deleted. Link to comment Share on other sites More sharing options...
Peter_17 Posted June 20 Author Share Posted June 20 Thanks for help. The hook actionProductDeleteAfter is not called after product deletion. This is the same situation as with hooks: actionAdminDeleteAfter actionAdminDeleteBefore actionAdminProductsControllerDeleteAfter actionAdminProductsControllerDeleteBefore and actionAdminProductDeleteAfter I checked that I can use other hooks like actionCategoryUpdate, actionListModules, actionAdminDuplicateAfter. For example the function hookActionCategoryUpdate is called correctly and json file is written to the directory. I tested it on the local installation and on the hosting configured for Prestashop. Link to comment Share on other sites More sharing options...
WisQQ Posted June 20 Share Posted June 20 (edited) 2 godziny temu, Peter_17 napisał: Thanks for help. The hook actionProductDeleteAfter is not called after product deletion. This is the same situation as with hooks: actionAdminDeleteAfter actionAdminDeleteBefore actionAdminProductsControllerDeleteAfter actionAdminProductsControllerDeleteBefore and actionAdminProductDeleteAfter I checked that I can use other hooks like actionCategoryUpdate, actionListModules, actionAdminDuplicateAfter. For example the function hookActionCategoryUpdate is called correctly and json file is written to the directory. I tested it on the local installation and on the hosting configured for Prestashop. Are you sure its registered correctly? Because Object hooks are allways called after save,add,delete action. Edit: Here is proper way, i'm using it myself but on PS 1.7.8 public function hookactionObjectProductDeleteBefore($params){ Edited June 20 by WisQQ (see edit history) Link to comment Share on other sites More sharing options...
Peter_17 Posted June 21 Author Share Posted June 21 On PS 1.7.8 this code works (json files are saved): public function install() { return parent::install() && $this->registerHook('actionObjectProductDeleteAfter') && $this->registerHook('actionObjectProductDeleteBefore'); } public function hookactionObjectProductDeleteAfter($params) { file_put_contents("../modules/testduplicate/dev_logs/array_params_after.json",json_encode($params)); } public function hookactionObjectProductDeleteBefore($params) { file_put_contents("../modules/testduplicate/dev_logs/array_params_before.json",json_encode($params)); } } but it does not work on PS 8.1.6 Which hooks should I use on PS 8.1.6? Link to comment Share on other sites More sharing options...
WisQQ Posted June 21 Share Posted June 21 38 minut temu, Peter_17 napisał: On PS 1.7.8 this code works (json files are saved): public function install() { return parent::install() && $this->registerHook('actionObjectProductDeleteAfter') && $this->registerHook('actionObjectProductDeleteBefore'); } public function hookactionObjectProductDeleteAfter($params) { file_put_contents("../modules/testduplicate/dev_logs/array_params_after.json",json_encode($params)); } public function hookactionObjectProductDeleteBefore($params) { file_put_contents("../modules/testduplicate/dev_logs/array_params_before.json",json_encode($params)); } } but it does not work on PS 8.1.6 Which hooks should I use on PS 8.1.6? Maybe the problem is file_put_contents function? Try to use built in prestashop logger. PrestaShopLogger::addLog('actionObjectProduct '.json_encode($params)); Link to comment Share on other sites More sharing options...
WisQQ Posted June 21 Share Posted June 21 public function delete() { // @hook actionObject<ObjectClassName>DeleteBefore Hook::exec('actionObjectDeleteBefore', ['object' => $this]); Hook::exec('actionObject' . $this->getFullyQualifiedName() . 'DeleteBefore', ['object' => $this]); $this->clearCache(); $result = true; // Remove association to multishop table if (Shop::isTableAssociated($this->def['table'])) { $id_shop_list = Shop::getContextListShopID(); if (count($this->id_shop_list)) { $id_shop_list = $this->id_shop_list; } $id_shop_list = array_map('intval', $id_shop_list); $result &= Db::getInstance()->delete($this->def['table'] . '_shop', '`' . $this->def['primary'] . '`=' . (int) $this->id . ' AND id_shop IN (' . implode(', ', $id_shop_list) . ')'); } // Database deletion $has_multishop_entries = $this->hasMultishopEntries(); // Database deletion for multilingual fields related to the object if (!empty($this->def['multilang']) && !$has_multishop_entries) { $result &= Db::getInstance()->delete($this->def['table'] . '_lang', '`' . bqSQL($this->def['primary']) . '` = ' . (int) $this->id); } if ($result && !$has_multishop_entries) { $result &= Db::getInstance()->delete($this->def['table'], '`' . bqSQL($this->def['primary']) . '` = ' . (int) $this->id); } if (!$result) { return false; } // @hook actionObject<ObjectClassName>DeleteAfter Hook::exec('actionObjectDeleteAfter', ['object' => $this]); Hook::exec('actionObject' . $this->getFullyQualifiedName() . 'DeleteAfter', ['object' => $this]); return $result; } This is delete function from objectmodel in PS8. So it should work exacly like on ps1.7 1 Link to comment Share on other sites More sharing options...
Peter_17 Posted June 22 Author Share Posted June 22 Thanks for help. Searching for a string: 'public function delete()' I found in a file /classes/Product.php a line: Hook::exec('actionProductDelete', ['id_product' => (int) $this->id, 'product' => $this]); So I wrote my code this way: public function install() { return parent::install() && $this->registerHook('actionProductDelete'); } public function hookactionProductDelete($params) { file_put_contents("../modules/testduplicate/dev_logs/array_params_APD.json",json_encode($params)); PrestaShopLogger::addLog('hookactionProductDelete'.json_encode($params)); } and it works I guess it's not 100% the same hook as 'actionProductDeleteAfter' but in my module it will work good enough. 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