Update user profile based on items ordered

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
4 months 1 day ago #362212

-- HikaShop version -- : 5.1.0
-- Joomla version -- : 5.0.3
-- PHP version -- : 8.2

I am creating a plugin to update my user profile based on items ordered.

I am using the onAfterOrderCreate trigger to execute my code. This seems to work fine when an order is created by the front end, but I noticed when creating an order from the back end, there is no product information in the $order array.

From the front end order, I have the $order->products array to get the item information. When the order is created from the back end, the onAfterOrderCreate is called before the products are added, so no product information is retrieved.

I can query the hikashop_order_product with the order_id to get what I need, but I need a trigger that fires after the products are added via the backend form.

Is there a better method to use?

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

  • Posts: 26156
  • Thank you received: 4028
  • MODERATOR
4 months 22 hours ago #362215

Hello,

Because "onAfterOrderCreate" is triggered when an order is created, it means that the order has mostly the "created" status and is not yet confirmed.
On the backend, when you create a new order, the order is fully empty, meaning that there is no user (yet).
Thus, the trigger is called in the backend but because that order is empty, you can't check the user nor the ordered items (there are none).

That's why, most of plugins are using the trigger "onAfterOrderUpdate" and check that the "order_status" is changing (between the current and the $order->old) to a "confirmed" status.
It allows to perform an action when you know that the order is beeing paid/confirmed ; and you can do the opposite actions when an order is refunded (leaving the "confirmed" statuses, to another group of order status).

By doing that, you will be also compatbile with the backend since you will create an order order, assign a user, add products and then changing the order status to "created" to "confirmed".

Regards,


Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Also helping the HikaShop support team when having some time or couldn't sleep.
By the way, do not send me private message, use the "contact us" form instead.

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

  • Posts: 51
  • Thank you received: 3
  • Hikashop Business
3 months 4 weeks ago #362254

That works for me.

Thank you

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

Time to create page: 0.061 seconds
Powered by Kunena Forum