andtrx Posted July 21, 2022 Share Posted July 21, 2022 (edited) Hello, I need 2 hooks, and no matter which ones I try, they dont seem to work. 1. I need a hook for whenever an order is edited from the backoffice. When the order changes status and when the admin ads or removes a product and saves the order. 2. I need another hook for whenever a new product is added / edited from the backoffice. My module looks as follows: public function install() { if (parent::install() && $this->registerHook('displayHeader') && $this->registerHook('displayOrderConfirmation') && $this->registerHook('updateOrderStatus') && $this->registerHook('addproduct') ) { return true; } return false; } public function hookdisplayOrderConfirmation($params) { $externalIdMobilPay = str_replace('%', '#', Tools::getValue('orderId')); $externalIdEuPlatesc = Tools::getValue('ep_id'); if (isset($externalIdMobilPay)) { $externalId = $externalIdMobilPay; } else if (isset($externalIdEuPlatesc)) { $externalId = $externalIdEuPlatesc; } else { $externalId = ""; } $cartId = $params['objOrder']->id; $order = new SeniorErpSendOrder; $order->createOrder($cartId, $externalId); } public function hookupdateOrderStatus($params) { die('exit'); } public function hookaddproduct($params) { die('exit'); } The hookdisplayOrderConfirmation works without any problem. The other two, dont I also tried re-installing the module. I found the hooks here: https://mypresta.eu/en/art/developer/prestashop-hook-list.html Thanks, Edited July 21, 2022 by andtrx (see edit history) Link to comment Share on other sites More sharing options...
Ali Samie Posted July 21, 2022 Share Posted July 21, 2022 (edited) Hi. It is recommended to look for the official docs, this is the list of hooks for prestashop 1.7 and you might be able to use most of them. Just take a look at: https://devdocs.prestashop.com/1.7/modules/concepts/hooks/list-of-hooks/ actionObject<ObjectName>UpdateAfter eg: actionObjectOrderUpdateAfter This hook gets fired after an existing Order gets updated in any way. actionObjectOrderHistoryAddAfter This hook gets fired after a new Order History is added. Just after order status update. actionObjectProductUpdateAfter This hook gets fired after an existing Product gets updated. Edited July 21, 2022 by stifler97 (see edit history) 1 Link to comment Share on other sites More sharing options...
andtrx Posted July 22, 2022 Author Share Posted July 22, 2022 @stifler97 so based on what you re saying, this should work: public function install() { if (parent::install() && $this->registerHook('displayHeader') && $this->registerHook('displayOrderConfirmation') && $this->registerHook('updateOrderStatus') && $this->registerHook('actionOrderEdited') && $this->registerHook('actionObjectProductUpdateAfter')) { return true; } return false; } public function hookactionObjectProductUpdateAfter($params) { die('updated product'); } But it does not ... nothing happens when I go into a product and I change its description for example Link to comment Share on other sites More sharing options...
Ali Samie Posted July 22, 2022 Share Posted July 22, 2022 3 minutes ago, andtrx said: But it does not ... nothing happens when I go into a product and I change its description for example Did you follow the rules? https://devdocs.prestashop.com/1.7/modules/concepts/hooks/ Just read the documentation and do as they say. If there is a problem do not hesitate to ask more questions. Link to comment Share on other sites More sharing options...
andtrx Posted July 25, 2022 Author Share Posted July 25, 2022 On 7/22/2022 at 11:42 AM, stifler97 said: Did you follow the rules? https://devdocs.prestashop.com/1.7/modules/concepts/hooks/ Just read the documentation and do as they say. If there is a problem do not hesitate to ask more questions. Hey, Yes I did read those but for some reason for me hooks like this hookActionObjectProductAddBefore its not working (if I add the classname is not working). So I tried to hack it using the regular:"Hook::exec('actionObjectAddBefore', array('object' => $this));" which is working What I am trying to do is execute some code after a new combination is added, first I store a cookie with current combinations, and in this function:" public function hookActionObjectAddAfter($params)" I am getting the combinations again and trying to check if a new one was created. Everything works fine except the fact that the combination code, is executed after the function below.. therfore I always get the same combination." public function hookActionObjectAddAfter($params)" public function hookActionObjectAddBefore($params) { if ($params['object']->object_type === "Product") { $product_obj = new Product( $params['object']->object_id, false, $language, $this->context->shop->id); $combinations = $product_obj->getAttributeCombinations( $language ); $parentProduct['productId'] = $params['object']->object_id; foreach($combinations as $combination) { $parentProduct['combination'][$combination['reference']]['id_attribute'] = $combination['id_attribute']; $parentProduct['combination'][$combination['reference']]['id_product_attribute'] = $combination['id_product_attribute']; } $cookie = new Cookie('existsNewComibinationProduct'); $cookie->setExpire(time() + 21 * 60); $cookie->existsNewComibinationProduct = json_encode($parentProduct); $cookie->write(); } } public function hookActionObjectAddAfter($params) { if ($params['object']->object_type === "Product") { $cookie = new Cookie('existsNewComibinationProduct'); $existsNewComibinationProduct = json_decode($cookie->existsNewComibinationProduct, true); $product = new Product( $existsNewComibinationProduct['productId'], false, 2); $combinations = $product->getAttributeCombinations(2); if ( count($combinations)+1 > count($existsNewComibinationProduct['combination']) && $params['object']->object_id == $existsNewComibinationProduct['productId']) { $items = []; foreach ($combinations as $combination) { if (!array_key_exists($combination['reference'], $existsNewComibinationProduct['combination'])) { $name = $combination['group_name'] . ' - ' . $combination['attribute_name']; $items[$combination['id_product_attribute']]["name"] = htmlspecialchars($product->name . ' (' . $name . ')'); $items[$combination['id_product_attribute']]["reference"] = $combination['reference']; $items[$combination['id_product_attribute']]["minimal_quantity"] = $combination['minimal_quantity']; } } } echo "<pre>"; print_r($items); echo "</pre>"; die; } } I am currently stuck. This is my ObjectModel.php (in case it helps anyone) public function add($autodate = true, $null_values = false) { if (!ObjectModel::$db) ObjectModel::$db = Db::getInstance(); if (isset($this->id) && !$this->force_id) unset($this->id); // @hook actionObject*AddBefore Hook::exec('actionObjectAddBefore', array('object' => $this)); Hook::exec('actionObject'.get_class($this).'AddBefore', array('object' => $this)); // Automatically fill dates if ($autodate && property_exists($this, 'date_add')) $this->date_add = date('Y-m-d H:i:s'); if ($autodate && property_exists($this, 'date_upd')) $this->date_upd = date('Y-m-d H:i:s'); if (Shop::isTableAssociated($this->def['table'])) { $id_shop_list = Shop::getContextListShopID(); if (count($this->id_shop_list) > 0) $id_shop_list = $this->id_shop_list; } // Database insertion if (Shop::checkIdShopDefault($this->def['table'])) $this->id_shop_default = min($id_shop_list); if (!$result = ObjectModel::$db->insert($this->def['table'], $this->getFields(), $null_values)) return false; // Get object id in database $this->id = ObjectModel::$db->Insert_ID(); // Database insertion for multishop fields related to the object if (Shop::isTableAssociated($this->def['table'])) { $fields = $this->getFieldsShop(); $fields[$this->def['primary']] = (int)$this->id; foreach ($id_shop_list as $id_shop) { $fields['id_shop'] = (int)$id_shop; $result &= ObjectModel::$db->insert($this->def['table'].'_shop', $fields, $null_values); } } if (!$result) return false; // Database insertion for multilingual fields related to the object if (!empty($this->def['multilang'])) { $fields = $this->getFieldsLang(); if ($fields && is_array($fields)) { $shops = Shop::getCompleteListOfShopsID(); $asso = Shop::getAssoTable($this->def['table'].'_lang'); foreach ($fields as $field) { foreach (array_keys($field) as $key) if (!Validate::isTableOrIdentifier($key)) throw new PrestaShopException('key '.$key.' is not table or identifier, '); $field[$this->def['primary']] = (int)$this->id; if ($asso !== false && $asso['type'] == 'fk_shop') { foreach ($shops as $id_shop) { $field['id_shop'] = (int)$id_shop; $result &= ObjectModel::$db->insert($this->def['table'].'_lang', $field); } } else $result &= ObjectModel::$db->insert($this->def['table'].'_lang', $field); } } } // @hook actionObject*AddAfter Hook::exec('actionObjectAddAfter', array('object' => $this)); Hook::exec('actionObject'.get_class($this).'AddAfter', array('object' => $this)); return $result; } Link to comment Share on other sites More sharing options...
Ali Samie Posted July 25, 2022 Share Posted July 25, 2022 if by hook is not working you mean that function does not fire after the event of actionObjectProductAddAfter, then the problem is that this event has not taken place at all. Check the ObjectModel class and search for Hook::exec You will see which hooks are there and when they get fired. AddAfter and UpdateAfter are different. The last thing is that, you mentioned you want to run some code or process some thing or any thing of that sort, JUST AFTER a combination is added. Read this again, a combination, so that will obviously not fire the ProductAddAfter at all. You need to find out the object models that are involved in the action of adding a product combination. Then put your code in that hook method. Thats it. Good luck Link to comment Share on other sites More sharing options...
andtrx Posted July 25, 2022 Author Share Posted July 25, 2022 4 hours ago, stifler97 said: if by hook is not working you mean that function does not fire after the event of actionObjectProductAddAfter, then the problem is that this event has not taken place at all. Check the ObjectModel class and search for Hook::exec You will see which hooks are there and when they get fired. AddAfter and UpdateAfter are different. The last thing is that, you mentioned you want to run some code or process some thing or any thing of that sort, JUST AFTER a combination is added. Read this again, a combination, so that will obviously not fire the ProductAddAfter at all. You need to find out the object models that are involved in the action of adding a product combination. Then put your code in that hook method. Thats it. Good luck Yeah I know, the weird thing is that now all the hooks fired, the same hooks I spent horus in the past days to get it work, and I did not change anyhting. I am really curious if this was a server side issue (with the cache maybe) the server is not hosted by me and I do not have full acccess. Either way, all hooks are firing properly now.. Link to comment Share on other sites More sharing options...
andtrx Posted July 26, 2022 Author Share Posted July 26, 2022 (edited) 19 hours ago, stifler97 said: if by hook is not working you mean that function does not fire after the event of actionObjectProductAddAfter, then the problem is that this event has not taken place at all. Check the ObjectModel class and search for Hook::exec You will see which hooks are there and when they get fired. AddAfter and UpdateAfter are different. The last thing is that, you mentioned you want to run some code or process some thing or any thing of that sort, JUST AFTER a combination is added. Read this again, a combination, so that will obviously not fire the ProductAddAfter at all. You need to find out the object models that are involved in the action of adding a product combination. Then put your code in that hook method. Thats it. Good luck @stifler97 1 last question, I fixed the code, and I am hooking into hookActionObjectCombinationAddAfter, Where I execute this code: public function hookActionObjectCombinationAddAfter($params) { $language = Tools::getValue('language', 'ro'); $language = Language::getIdByIso( $language ); $product = new Product( $params['object']->id_product, false, $language); $combinations = $product->getAttributeCombinations(2); $newArr = []; foreach($combinations as $combination) { $attributeValues = $product->getAttributeCombinationsById($combination['id_product_attribute'], 2); $newArr[$combination['id_product_attribute']][] = $attributeValues; } echo "<pre>"; print_r($newArr); echo "</pre>"; die("exit"); } I expected the newsest combination added (the one with an ID of [2846]) to return the same values as the combination with the id of [5], but for some reason [attribute_name] is empty. Is there another hook that gets executed after the hook I am using, and therfore the attribute_name is created after? I also tried hookActionObjectAttributeGroupAddAfter but it does not trigger. [5] => Array ( [0] => Array ( [0] => Array ( [id_product_attribute] => 5 [id_product] => 1219 [reference] => ART002279SED [supplier_reference] => [location] => [ean13] => [upc] => [wholesale_price] => 0.000000 [price] => 0.000000 [price_euro] => 0.000000 [ecotax] => 0.000000 [quantity] => 99998 [weight] => 0.000000 [unit_price_impact] => 0.00 [default_on] => 1 [minimal_quantity] => 1 [available_date] => 0000-00-00 [sedona_custom_status] => 0 [sedona_stock_ro] => In stoc la depozit [sedona_stock_en] => Warehouse stock [sedona_delivery_ro] => Maximum 48 de ore [sedona_delivery_en] => Maximum 48 hours [id_shop] => 1 [id_attribute_group] => 2 [is_color_group] => 0 [group_name] => Brat flexibil [attribute_name] => Elbow Arm [id_attribute] => 5 ) ) ) [2846] => Array ( [0] => Array ( [0] => Array ( [id_product_attribute] => 2846 [id_product] => 1219 [reference] => 1231 [supplier_reference] => [location] => [ean13] => [upc] => [wholesale_price] => 0.000000 [price] => 0.000000 [price_euro] => 0.000000 [ecotax] => 0.000000 [quantity] => 0 [weight] => 0.000000 [unit_price_impact] => 0.00 [default_on] => 0 [minimal_quantity] => 1 [available_date] => 0000-00-00 [sedona_custom_status] => 0 [sedona_stock_ro] => [sedona_stock_en] => [sedona_delivery_ro] => [sedona_delivery_en] => [id_shop] => 1 [id_attribute_group] => [is_color_group] => [group_name] => [attribute_name] => [id_attribute] => ) ) ) Edited July 26, 2022 by andtrx (see edit history) 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