- Posts: 25
- Thank you received: 2
New event suggestion
5 years 2 months ago #334422
by braverman
New event suggestion was created by braverman
-- url of the page with the problem -- :
www.starbrite.cz/
-- HikaShop version -- : 4.4.3
-- Joomla version -- : 3.9.28
-- PHP version -- : 7.4.20
Hi,
I have some suggestion - to add new event during updating of product quantity when saving order. I have some actions that has to be done on each order product save and so I made a hack now that is firing an event in method updateQuantityAndSales of class order_product.php. It looks like this:
And of course I am using it in my plugin.
Does it make sense or is it completely out of sense and there is better way how to do it. If it looks meaningful for you please consider to implement it in official code.
Thanks,
Jan
-- HikaShop version -- : 4.4.3
-- Joomla version -- : 3.9.28
-- PHP version -- : 7.4.20
Hi,
I have some suggestion - to add new event during updating of product quantity when saving order. I have some actions that has to be done on each order product save and so I made a hack now that is firing an event in method updateQuantityAndSales of class order_product.php. It looks like this:
Code:
if($localCancel) {
$query = 'UPDATE '.hikashop_table('product').' SET product_quantity = product_quantity + '.(int)$k.' WHERE product_id IN ('.implode(',',$update).') AND product_quantity > -1';
} else {
$query = 'UPDATE '.hikashop_table('product').' SET product_quantity = GREATEST(0, product_quantity - '.(int)$k.') WHERE product_id IN ('.implode(',',$update).') AND product_quantity >= 0';
}
$this->database->setQuery($query);
$this->database->execute();
// QN hack
// 2021-07-14
JPluginHelper::importPlugin( 'hikashop' );
$app = JFactory::getApplication();
$app->triggerEvent('onAfterOrderProductSave', array($k, $update, $localCancel) );
// END QN hack
And of course I am using it in my plugin.
Does it make sense or is it completely out of sense and there is better way how to do it. If it looks meaningful for you please consider to implement it in official code.
Thanks,
Jan
Please Log in or Create an account to join the conversation.
5 years 2 months ago #334423
by braverman
Replied by braverman on topic New event suggestion
Of course it would be better to take importPlugin and getApplication out of loop:)
Please Log in or Create an account to join the conversation.
5 years 2 months ago #334425
by nicolas
Replied by nicolas on topic New event suggestion
Hi,
Thanks for your feedback.
Adding a trigger in the updateQuantityAndSales function makes sense.
Now, I can't agree with the name of the trigger event since the saving in hikashop_order_product is actually done in the save function of the same file, after the call to updateQuantityAndSales.
Also, I would prefer to add the trigger outside of the loop. In fact, I would rather add one before the loop and one after the loop, like so:
That way, plugins can even cancel the stock update by emptying the $updates array in the onBeforeProductStockUpdate event.
What do you think ? Does this still work for the custom plugin you're working on ?
Thanks for your feedback.
Adding a trigger in the updateQuantityAndSales function makes sense.
Now, I can't agree with the name of the trigger event since the saving in hikashop_order_product is actually done in the save function of the same file, after the call to updateQuantityAndSales.
Also, I would prefer to add the trigger outside of the loop. In fact, I would rather add one before the loop and one after the loop, like so:
Code:
JPluginHelper::importPlugin('hikashop');
$app = JFactory::getApplication();
$app->triggerEvent('onBeforeProductStockUpdate', array(&$updates, $cancel));
if(!empty($updates)) {
... loop code ...
$app->triggerEvent('onAfterProductStockUpdate', array(&$updates, $cancel));
}
What do you think ? Does this still work for the custom plugin you're working on ?
Please Log in or Create an account to join the conversation.
5 years 2 months ago #334474
by braverman
Replied by braverman on topic New event suggestion
Yes, that makes sense, way better than my suggestion. I can work with with no problem. Thanks!
Please Log in or Create an account to join the conversation.
5 years 2 months ago #334476
by nicolas
Replied by nicolas on topic New event suggestion
Hi,
Great. We'll add that for the next version.
Great. We'll add that for the next version.
Please Log in or Create an account to join the conversation.
5 years 2 months ago #334484
by braverman
Replied by braverman on topic New event suggestion
Super, thanks!
Please Log in or Create an account to join the conversation.
Time to create page: 0.284 seconds