hoping someone can help with this. It seems like a simple problem but wondering if there's a cleaner way to do it.
Right now I have a hook that runs whenever a WooCommerce order is completed and when a order is updated. That hook is the following:
add_action('save_post_shop_order', 'printout', 10, 3);
function printout($post_ID, $post, $update)
{
$posttype = get_post_type($post_id);
write_to_log('Post Type:' . $posttype);
if (!is_admin()){
return;
}
if($update && get_post_type($post_id) === "shop_order"){
$msg = $post_ID;
$order = get_woocommerce_order($msg);
mainplugin($msg, $order);
}
}
add_action('woocommerce_thankyou', 'neworder_created', 10, 2);
function neworder_created($order_id){
$order = get_woocommerce_order($order_id);
mainplugin($order_id, $order);
}
The hook to run the plugin on order update works perfect. You'll notice that I have some validation logic in there:
if($update && get_post_type($post_id) === "shop_order"){
$msg = $post_ID;
$order = get_woocommerce_order($msg);
mainplugin($msg, $order);
}
This is essentially the functionality I'm trying to replicate for new orders. What happens is when folks make a purchase on my site, 4 database calls are made with the following post types in order of creation:
- wc_booking (child)
- shop_order (parent)
- wcdp_payment (child)
- wcdp_payment (child)
What's happening is the hook for a new order
add_action('woocommerce_thankyou', 'neworder_created', 10, 2);
is running on the last database call of a wcdp_payment instead of what I need it to; it should run only on that shop_order DB entry (or I guess the parent order is what I need). wcdp_payment is missing order information.
Is there a way to get it to run when a post type is ONLY shop_order? I know I could wire up some logic similar to how I have it set up to handle updates but my concern is that I don't know exactly what this hook is doing, so will it even reach the desired "shop_order" db entry? Or is there a different hook I should be using?
Thank you for your help! Hope this made sense.