I want to write a custom plugin that does some action after woocommerce order is completed, but I can’t get this hook to work. I can see this question asked many times.
Here: https://wordpress.org/support/topic/woocommerce_order_status_completed-is-not-working
And here: https://wordpress.org/support/topic/woocommerce_order_status_completed-action-hook-not-working
But I cannot help myself with answers that these guys received.
I tried to add the action a few different ways:
add_action( 'woocommerce_order_status_completed', 'ikwoocommerceorderstatuscompleted_func');
add_action( 'woocommerce_order_status_completed', array($this,'ikwoocommerceorderstatuscompleted_func'), 10, 1);
add_action( 'woocommerce_order_status_completed', array(&$this,'ikwoocommerceorderstatuscompleted_func'), 10, 1);
Also tried with a class:
class IKHooks {
function __construct() {
add_action( 'woocommerce_order_status_completed', array($this,'ikwoocommerceorderstatuscompleted_func'), 10, 1);
}
public function ikwoocommerceorderstatuscompleted_func( $order_id ) {
}
}
I even tried to put the action outside of the class:
add_action( 'woocommerce_order_status_completed', array(IKHooks,'ikwoocommerceorderstatuscompleted_func'), 10, 1);
None of these examples work. 🙁
Check the following steps before calling your hook.
Check if order completion email is sent.
Hook is properly registered in plugin file or theme
functions.php
Check if the name of your callback function is in the hook array:
If you find it then everything is ok, it means that probably there’s an issue with your theme or
functions.php
file. Check for the hook or callback function in your files and then look forremove_action
orremove_all_actions
that’s probably what’s preventing your hook from being called.You can also check in this way
Change the priority of your hook from 10 to 1 so it is called first than any other action or hook.
You can use this hook
And the function will look like
Hope this will be helpful.
I faced the similar issue earlier and solved it as:
Following code update the order status as
completed
Once order is completed, if you want to do something, see the following code:
Hopefully, it will work 🙂
woocommerce_order_status_changed
andwoocommerce_order_status_completed
actually work for me. After struggling for 2 days i realized that you just can’t var_dump or var_export or print_r or whatever in the admin panel, it just won’t work.So if you are a newbie like me and thought those actions weren’t working, just try triggering another action like sending a mail for example.
This code works:
Try using action hook
woocommerce_order_status_changed
.It takes 4 parameters. order id, old status new status and order. Further code reference HERE
This may not be exactly suitable to your requirement, but seems to be worth of an alternative. Hope this helps.
I think you might be after this guy.
woocommerce_payment_complete