New event suggestion

  • Posts: 10
  • Thank you received: 0
  • Hikashop Business
3 weeks 12 hours ago #334422

-- 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:

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.

  • Posts: 10
  • Thank you received: 0
  • Hikashop Business
3 weeks 12 hours ago #334423

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.

  • Posts: 73432
  • Thank you received: 11245
  • MODERATOR
3 weeks 4 hours ago #334425

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:

		JPluginHelper::importPlugin('hikashop');
		$app = JFactory::getApplication();
		$app->triggerEvent('onBeforeProductStockUpdate', array(&$updates, $cancel));

		if(!empty($updates)) {
... loop code ...

			$app->triggerEvent('onAfterProductStockUpdate', array(&$updates, $cancel));
		}
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 ?

Please Log in or Create an account to join the conversation.

  • Posts: 10
  • Thank you received: 0
  • Hikashop Business
2 weeks 6 days ago #334474

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.

  • Posts: 73432
  • Thank you received: 11245
  • MODERATOR
2 weeks 5 days ago #334476

Hi,

Great. We'll add that for the next version.

Please Log in or Create an account to join the conversation.

  • Posts: 10
  • Thank you received: 0
  • Hikashop Business
2 weeks 5 days ago #334484

Super, thanks!

Please Log in or Create an account to join the conversation.

Time to create page: 0.066 seconds
Powered by Kunena Forum