Luigi Donato Posted July 1, 2022 Share Posted July 1, 2022 Greetings, I would like to perform PHP/SQL actions after saving a product sheet, so I should hook into the actionProductUpdate action. Reading through the various discussions here and elsewhere, I've only found the solution via module, while I'd rather create a simple override to add a few lines of code to the actionProductUpdate function. It is feasible? Link to comment Share on other sites More sharing options...
Ress Posted July 1, 2022 Share Posted July 1, 2022 (edited) I prefer the option of using a module. You can install the module on other prestashops, or you can disable the function by just deactivating the module. But if it's something very specific to that shop, maybe it's more ok to do direct override. Edited July 1, 2022 by Ress (see edit history) Link to comment Share on other sites More sharing options...
Luigi Donato Posted July 2, 2022 Author Share Posted July 2, 2022 (edited) Ok @Ress, yes direct override is what I would like to do, but how do you do it..? This is the question of the thread 🙂 Edited July 2, 2022 by Luigi Donato (see edit history) Link to comment Share on other sites More sharing options...
knacky Posted July 3, 2022 Share Posted July 3, 2022 Can you be specific? What do you want to add? Edit the update hook in ./classes/Product.php, or add your own there. public function update($null_values = false) { $return = parent::update($null_values); $this->setGroupReduction(); // Sync stock Reference, EAN13 and UPC if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && StockAvailable::dependsOnStock($this->id, Context::getContext()->shop->id)) { Db::getInstance()->update('stock', array( 'reference' => pSQL($this->reference), 'ean13' => pSQL($this->ean13), 'isbn' => pSQL($this->isbn), 'upc' => pSQL($this->upc), ), 'id_product = ' . (int) $this->id . ' AND id_product_attribute = 0'); } Hook::exec('actionProductSave', array('id_product' => (int) $this->id, 'product' => $this)); Hook::exec('actionProductUpdate', array('id_product' => (int) $this->id, 'product' => $this)); if ($this->getType() == Product::PTYPE_VIRTUAL && $this->active && !Configuration::get('PS_VIRTUAL_PROD_FEATURE_ACTIVE')) { Configuration::updateGlobalValue('PS_VIRTUAL_PROD_FEATURE_ACTIVE', '1'); } return $return; } 1 Link to comment Share on other sites More sharing options...
lordignus Posted July 4, 2022 Share Posted July 4, 2022 I'd agree that doing it via a module is better than editing core code. Doesn't have to be anything fancy. This would do it 🤷♀️ <?php if (!defined('_PS_VERSION_')) exit; class CustomAfterProductUpdate extends Module { public function __construct() { $this->name = 'customafterproductupdate'; $this->tab = 'others'; $this->version = '0.0.1'; $this->author = 'John Doe'; $this->need_instance = 0; $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_); parent::__construct(); $this->displayName = $this->l('Custom After Product Update'); $this->description = $this->l('Run custom code after product update'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall?'); } public function install() { if (!parent::install() || !$this->registerHook('actionProductUpdate') ) return false; return true; } public function uninstall() { if (!parent::uninstall()) return false; return true; } public function hookActionProductUpdate($params) { /* YOUR CODE HERE */ } } In the hookActionProductUpdate method you can access product id with $params['id_product'] and the full Product object with $params['product'] Link to comment Share on other sites More sharing options...
Luigi Donato Posted July 5, 2022 Author Share Posted July 5, 2022 Hi sir and thanks for your reply, but as I specified in the first post, I would like to run these few lines of code without going through the module. What I would like to do I would like to do it via an override. Link to comment Share on other sites More sharing options...
knacky Posted July 5, 2022 Share Posted July 5, 2022 (edited) And why do you want to go with your head against the wall? By overriding you can get many errors after upgrade. @lordignus gave you the full code of the module. Can you tell us what exactly you want to save in the product database after the Update event? Edited July 5, 2022 by knacky (see edit history) Link to comment Share on other sites More sharing options...
knacky Posted July 6, 2022 Share Posted July 6, 2022 Hooks before and after the update can also be used. public function generateRandomString($length = 10) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } public function hookActionObjectProductUpdateAfter($object) { $idProduct = $object['object']->id; /* your code here */ /* SAMPLE, update reference and deactive */ Db::getInstance()->update('product', array('reference' => 'ABCDEFG', 'active' => (int)'0'), 'id_product' => (int)$idproduct); Db::getInstance()->update('product_shop', array('active' => (int)'0'), 'id_product' => (int)$idproduct); } public function hookActionObjectProductUpdateBefore($object) { $idProduct = $object['object']->id; /* your code here */ /* SAMPLE, update reference (generated automatically) and deactive */ Db::getInstance()->update('product', array('reference' => $this->generateRandomString(), 'active' => (int)'0'), 'id_product' => (int)$idproduct); Db::getInstance()->update('product_shop', array('active' => (int)'0'), 'id_product' => (int)$idproduct); } Link to comment Share on other sites More sharing options...
Luigi Donato Posted July 7, 2022 Author Share Posted July 7, 2022 I want to record timestamp and id of the employee who entered/updated the product, in a new DB table. Link to comment Share on other sites More sharing options...
knacky Posted July 7, 2022 Share Posted July 7, 2022 (edited) And why didn't you write it in the first question? You should already have the solution ready. This is how we write codes for you here and nothing like that. In my example, you are clearly shown how to solve it. If you don't know how to create a module, or you don't know the basics of PHP, please enter a request in the JOB section. It's a 30 minute job for a programmer to create a module. Your table: ps_my_table_name public function hookActionObjectProductUpdateAfter($object) { $idProduct = $object['object']->id; $idEmployee = $this->context->employee->id; if ($idEmployee && $idProduct){ $employee = new Employee((int)$idEmployee); $getEmployeeName = $employee->firstname.' '.$employee->lasname; Db::getInstance()->insert('my_table_name', array('id_product' => (int)$idProduct, 'id_employee' => $idEmployee, 'employee_name' => pSQL($getEmployeeName), 'date_upd' => pSQL(date('Y-m-d H:i:s'))); } } Edited July 7, 2022 by knacky (see edit history) Link to comment Share on other sites More sharing options...
Luigi Donato Posted July 8, 2022 Author Share Posted July 8, 2022 19 hours ago, knacky said: And why didn't you write it in the first question? You should already have the solution ready. This is how we write codes for you here and nothing like that. In my example, you are clearly shown how to solve it. If you don't know how to create a module, or you don't know the basics of PHP, please enter a request in the JOB section. It's a 30 minute job for a programmer to create a module. Your table: ps_my_table_name public function hookActionObjectProductUpdateAfter($object) { $idProduct = $object['object']->id; $idEmployee = $this->context->employee->id; if ($idEmployee && $idProduct){ $employee = new Employee((int)$idEmployee); $getEmployeeName = $employee->firstname.' '.$employee->lasname; Db::getInstance()->insert('my_table_name', array('id_product' => (int)$idProduct, 'id_employee' => $idEmployee, 'employee_name' => pSQL($getEmployeeName), 'date_upd' => pSQL(date('Y-m-d H:i:s'))); } } I did not write it because if you had told me how to create an override on that action, then I knew how to proceed, I would not have expected you to write the code for me. I have already created other modules and I'm a PHP programmer, so that's not the problem, the point is just that this time I didn't want to use a module but an override, also to gain experience, but as far as I understand I will not receive any answers about it, as you continue to insist on creating a module. Link to comment Share on other sites More sharing options...
knacky Posted July 8, 2022 Share Posted July 8, 2022 I wrote you the reason why no override. It is not well maintainable between Prestashop versions and causes errors after upgrading Prestashop. I also showed you where to put some of your code in the override. Link to comment Share on other sites More sharing options...
Luigi Donato Posted July 8, 2022 Author Share Posted July 8, 2022 28 minutes ago, knacky said: I wrote you the reason why no override. It is not well maintainable between Prestashop versions and causes errors after upgrading Prestashop. I also showed you where to put some of your code in the override. If I understand correctly, writing an override means copying the entire code, can't just write the extra lines..? Link to comment Share on other sites More sharing options...
knacky Posted July 8, 2022 Share Posted July 8, 2022 ????? Now I don't understand the question ????? Link to comment Share on other sites More sharing options...
knacky Posted July 8, 2022 Share Posted July 8, 2022 File ./override/classes/Product.php Link to comment Share on other sites More sharing options...
Luigi Donato Posted July 9, 2022 Author Share Posted July 9, 2022 21 hours ago, knacky said: File ./override/classes/Product.php I mean that the update function must be copied before adding custom lines. I can't just tell PrestaShop, run this code at the end or at the beginning, without first copying the entire code.. And if the code changes during an update, I'll have to manually intervene. 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