data missing after onAfterOrderUpdate

  • Posts: 290
  • Thank you received: 22
1 year 7 months ago #350612

-- url of the page with the problem -- : demo-hk.dlidev.nl
-- HikaShop version -- : 4.7.2
-- Joomla version -- : 4.2.9
-- PHP version -- : 8.0

Hi,
When an order status is changed to shipped via the Dashboard > Orders > Orders list using the dropdown the the data received is different from doing this via Dashboard > Orders > click on order number > change Order status via "Main information / Order status".

The fields missing for example are :
order_user_id
order_billing_address_id
order_billing_address_id
order_lang
order_full_price

We need these for our product_reviews extension.

Two files with output are in the attachement.

File Attachment:

File Name: HikaShopon...data.zip
File Size:4 KB

Attachments:

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 7 months ago #350613

Hi,

In onAfterOrderUpdate, the data you have in $element is the order that is being modified by the save.
If the data is missing it just means that the code calling the save doesn't want to update that data.
For example, if a payment plugin automatically changes the status of the order after the payment, it only provides the order_id and the new order_status (HikaShop will add automatically the order_modified, and maybe other elements like the order_invoice_id, order_invoice_number if the invoice number is being generated).

So in onAfterOrderUpdate, if you want to use information about an order, you need to do two things:
- look directly in $element and if it's there, use it
- look in $element->old . There, you have all the data of the order from before the save.
So for example, suppose you want to check the order_type to only take into account main orders (and not sub orders of HikaMarket), you would need to do it like this:

public function onAfterOrderUpdate(&$element, &$send_email) {
 // only take into account main orders
 $type = $element->old->order_type;
 if(!empty($element->order_type))
  $type = $element->order_type;
 if($type != 'sale')
  return;

 // process the order changes
}

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

  • Posts: 29
  • Thank you received: 2
1 year 7 months ago #350627

Hi Nicolas,

I work with Nico on this project, so let me follow up on his question.

3rd party plugins aside, there are two ways the order status can be changed:

  1. Directly from within the order lis view.
  2. When editing an order.
It's these two situations Nico refers to. To be honest, I don´t quite understand why the data I receive in the onAfterOrderUpdate event handler, is not exactly the same in both cases. After all, in both cases it is exactly the same order and the event is triggered by HikaShop itself.

I am also not quite sure how $element->old is of any help here. Unless the data in $element is always exactly the same as that of $element->old, in which case $element->old is redundant. What am I missing here?

Interesting and helpful is your mention of 3rd party plugins. This has not been taken into account in our own plugin yet, so we must pay attention to that. If only minimal amount of order data is provided in the onAfterOrderUpdate event handler, I guess we must get hold of the missing information ourselves. What would be the correct way to do that; to obtain all data about the order and the associated customer?

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 7 months ago #350628

Hi,

Mmm, I already answered all the questions of your new message in my previous message, so I'm not sure what to say.

In $element->old you will get all the data of the order before the save, always.
In $element you will get the changing data of the order.

So for example, if you want to get the language of the order, you can do it like this:
$lang = $element->old->order_lang;
if(!empty($element->order_lang))
$lang = $element->order_lang;

That way, in $lang you'll get the new order_lang if the lang is being changed by the save, or the order_lang from the database if it's not.
So onAfterOrderUpdate does provide all the data of the order, and you don't need to do anything to get the data not in $element. Just look in $element->old

The fact that you might get all the order data in $element is some cases is just luck based on how the person who wrote the code handled the saving. In a plugin, you should assume that $element doesn't have everything, and you need to use $element->old when what you want is missing in $element

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

  • Posts: 29
  • Thank you received: 2
1 year 7 months ago #350640

Hi Nicolas,

Perhaps you did answer all the questions, but it still was not entirely clear to me. I am not very quick-witted ;) I think I understand now, allthought it still feels kind of awkward.

Thanx for your help.

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

Time to create page: 0.077 seconds
Powered by Kunena Forum