Skip to content
Advertisement

Trying to find the right hook for WooCommerce Order Submission

I have a plugin I developed that connects a WooCommerce order to HubSpot. The issue i’m running into though is that while it works, the hook im using right now is sending order info to HubSpot before its technically complete. So this means that stuff like Failed orders get sent in as Pending, and coupon codes are ommitted.

So i’m wondering what the right hook to use is.

My goal: Everytime a WooCommerce order is created & completed and a WooCommerce order is updated, send the data to HubSpot.

What I have so far:

add_action('save_post_shop_order', 'printout', 10, 3);

function printout($post_ID, $post, $update)
{
    if (!is_admin()){
        return;
    }

    if($update){
        $msg = $post_ID;
        $order = get_woocommerce_order($msg);
        mainplugin($msg, $order);
    }

}


add_action('woocommerce_new_order', 'neworder_delegator', 10, 2);


function neworder_delegator($order_id, $order){
    mainplugin($order_id, $order);
}

So I guess i’m just looking for the right hook to get me what I want.

Thanks!

Advertisement

Answer

Here is your answer:

Each WooCommerce Order transition has one or more dynamic hooks that trigger when the status transition happens.

They start with ‘woocommerce_order_status_’ and the remaining part of the action is either the new status that the order has transitioned to, or the to and from statuses involved in the format ‘<status_transition_from>to<status_transition_to>’

Examples

You can hook your function to

add_action( 'woocommerce_order_status_completed', 'your_order_completed_function');

To trigger your function only when the order transitioned to completed, and not when refunded, cancelled, on-hold, etc. as those would run on other actions such as

woocommerce_order_status_refunded
woocommerce_order_status_cancelled
woocommerce_order_status_on-hold
woocommerce_order_status_failed
woocommerce_order_status_processing

Edited to add link to official WooCommerce docs:

https://woocommerce.github.io/code-reference/hooks/hooks.html

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement