0

We have a plugin that uses the woocommerce_payment_complete hook which calculates an orders point value for our customers based on the items and order total. This generates a CSV file and then uses rsync to send the CSV file to an external system which stores and manipulates this data.

In the same plugin we also use the woocommerce_order_status_changed hook which recalculates the values depending on the new order status (refunded items etc), generates the CSV file and sends to the external source again.

This is all working fine, a customer can make an order and the CSV file is generated etc, we can update the order status in the admin dashboard manually and the new CSV file is generated and sent. However, if an order is automatically changed via WooCommerce or the payment gateway, it seems as though the woocommerce_order_status_changed hook is not firing as the CSV file is not being generated.

So my questions are:

  1. Does the woocommerce_order_status_changed hook get triggered when the order status is changed automatically, or is it only when the order status is changed manually in the backend by us?

  2. Is there a hook that is triggered when the order status is changed automatically via WooCommerce/payment gateways?

I've search high and low but every result seems to either point to what I'm already doing or is about setting up a script that changes the status using $order->update_status() and similar alternatives which is not what we're looking for.

One of the results did bring up this (in reference to woocommerce_order_status_changed):

"Where the hook is used (in WC core): Does not used."

Which seems to answer Question 1, which is what leads me to believe if WooCommerce or a payment gateway changes the order status automatically, it will never fire, thus not creating our CSV etc.

Can anyone shed some light on this? Is there a hook which we can trigger on order statuses changed automatically via WooCommerce or a payment gateway?

closed as off-topic by Krzysiek Dróżdż, Jacob Peattie, Howdy_McGee Feb 22 at 5:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2

One of the results did bring up this (in reference to woocommerce_order_status_changed):

"Where the hook is used (in WC core): Does not used."

Which seems to answer Question 1, which is what leads me to believe if WooCommerce or a payment gateway changes the order status automatically, it will never fire, thus not creating our CSV etc.

That's not what that means. What that's saying is that WooCommerce itself doesn't add any additional actions to that hook. Not that it doesn't get fired at all.

The woocommerce_order_status_changed hook fires whenever an order status is changed using the $order->update_status() or $order->set_status() methods. WooCommerce only uses these methods internally to change order statuses (and 3rd-party extensions are supposed to also). So if WooCommerce itself ever updates the order status, automatically or not, the hook will fire. This includes when updating the Order manually in the back-end.

The hook wouldn't fire if an extension, such as a payment gateway, tried to set the order status incorrectly. For example, if they changed the status by using $order->status = 'processing' or wp_transition_post_status( 'on-hold', 'processing', $order ); then the hook would not fire.

So you're going to need to find out what circumstances exactly the hook is not firing. If it's a payment gateway updating the order status, and the hook doesn't fire, then it's likely that its developers are doing the status change incorrectly. At that point you would need to talk to its developers.

  • Ah ok, that's helpful, thank you! Two examples then: One order used the PayPal gateway. Order Note: "Unpaid order cancelled – time limit reached. Order status changed from Pending payment to Cancelled." So this could be determined based on how PayPal change that status? The other was using the Stripe gateway. Order Note #1: "The card was declined." Order Note #2: "Order status changed from Pending payment to Failed." So you would recommend getting in touch with their support to see how they handle order status changes? Thanks again! – no. Feb 15 at 14:04
  • That's probably where I'd start, yes. I'd be surprised if those two gateways were doing it incorrectly though, since I believe those integrations are developed by or closely with the WooCommerce team. The issue could just be with your own code, if it's not using the hook correctly somehow. – Jacob Peattie Feb 15 at 14:06
  • Yeah I guess, just seems strange that the actions/hooks work when done manually but not automatically, surely if one change fires it the other would as well? Like you said, unless they are using wp_transition_post_status or something which would avoid it. – no. Feb 15 at 14:11
  • In regards to if I've got it set up correct... the plugin is a class, the constructor has the actions/hooks both link to the same method and the class is instantiated on the same page directly after its declaration. Only thing I can think of is the plugin/class isn't loaded with whatever script the gateway uses to update the status, which also wouldn't make sense since surely the plugin would be included as WordPress is loaded. – no. Feb 15 at 14:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.