Order status hook

  • Posts: 75
  • Thank you received: 4
1 year 7 months ago #351009

-- HikaShop version -- : 4.7.2
-- Joomla version -- : 4.2.9
-- PHP version -- : 8.0

I have some questions about order statuses, and payments.

I made a new order status called "parcel ready".
Questions:
1. When I choose this status, is there any hook which I can use to call an API to the shipping company. So I can check if the shipping company is myshippingcompany and the status is changed to "parcel ready" then call a php file which sends the data to the company.
I would need it when the payment method is wire transfer.
2. Also when the payment is through an online service and come back as completed I would need to call the same method automatically.

Where should I implement the changes (ideally one place in the code) to make it work?

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

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

Hi,

You want to develop a small plugin of the group "hikashop" and implement the onAfterOrderUpdate(&$order,&$send_email) event:
www.hikashop.com/support/documentation/6...#onBeforeOrderUpdate
This event is called each time something is changed in an order, including the order_status.
So you want to check $order->order_status and $order->old->order_status and based on this, make the call to the API.
For example:

class plgHikashopName extends JPlugin {
	function plgHikashopName(&$subject, $config){
		parent::__construct($subject, $config);
	}

	function onAfterOrderUpdate(&$order, &$send_email) {
		// order status is not being changed
		if(empty($order->order_status))
			return true;
		// order status is not being changed to "parcel ready"
		if($order->order_status!="parcel ready")
			return true;
		// order status was already before "parcel ready"
		if($order->order_status == $order->old->order_status)
			return true;

		// call the API
	}
}

I recommend doing it as a plugin. That way, you won't have anything to do each time you update HikaShop and this API will probably never change in the future, meaning that the plugin will work with future versions of HikaShop without doing anything.

The following user(s) said Thank You: Petike1007

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

  • Posts: 75
  • Thank you received: 4
1 year 7 months ago #351018

This sounds good...thanks
And can I trigger it somehow when the customer pays with card and the transaction is successful?

I would need to change the status in the payment plugin and trigger this event.
I have the succesful payment part of the payment plugin...

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

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

Hi,

When the customers pays with card and the transaction is successful, the payment gateway you're using will normally send an HTTP request to the payment plugin on your website, which will verify it and change the status of the order from "created" to "confirmed". And when it does that, it normally goes through the order save procedure in HikaShop which triggers the onAfterOrderUpdate event I was talking about above.
So normally, the onAfterOrderUpdate event should already be triggered in your plugin when this happens without doing anything special.

The following user(s) said Thank You: Petike1007

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

  • Posts: 75
  • Thank you received: 4
1 year 7 months ago #351026

How can I debug / try things like var_dump? What you wrote example is owrking, I can see, but tried to have a look what the $order object contains, but as it displays in a popup disappear in a second when I change the status.

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

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

Hi,

Add

exit;
after the var_dump. It will stop the processing and should display the var_dump in the popup without it closing.

Also, HikaShop has a function hikashop_writeToLog($data)
When you call that function, it will store the content of $data in the "payment log file" you can access under the Main tab of the HikaShop configuration. So it's a great way to debug things where you don't have a way to display things to the browser.
For example, when the payment gateway calls the payment plugin to notify it of the payment, this usually happens in a server-to-server way and thus if you var_dump something in the payment plugin, or a plugin acting upon the order status change, it will be output to the payment gateway server. So using hikashop_writeToLog() comes in handy.

The following user(s) said Thank You: Petike1007

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

  • Posts: 75
  • Thank you received: 4
1 year 6 months ago #351264

How can I get all the order details inside the onAfterOrderUpdate function?
I need the billing, shipping details but looks like it doesn't have these...

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 6 months ago #351271

Hi,

Indeed, in onAfterOrderUpdate you only get in $order what the thing saving the order is providing, not the complete order.
So if you need the complete order information, you can use :

$orderClass = hikashop_get('class.order');
$fullOrder = $orderClass->loadFullOrder($order->order_id, true, false);
And you'll get everything in $fullOrder

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

  • Posts: 75
  • Thank you received: 4
1 year 3 months ago #354354

Can I change order status in the plugin?
I use the onAfterOrderCreate(&$order) fuunction to connect the hikshop with the billing software.
Can I set the order status from "created" to "invoice_sent" when it returns true?
If so, does it run the onAfterOrderUpdate event or not?

Last edit: 1 year 3 months ago by Petike1007.

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

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 3 months ago #354356

Hi,

Yes you can use that code:

$newObject = new stdClass();
$newObject->order_id = $order->order_id;
$newObject->order_status = 'invoice_sent';
$orderClass = hikashop_get('class.order');
$orderClass->save($newObject);
This will silently (no email will be sent) change the order status.
And yes, onAfterOrderUpdate will be triggered.

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

Time to create page: 0.069 seconds
Powered by Kunena Forum