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:
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.
Hello dear stranger on the internet. I asked the computer, and this is what it came up with:
add_action( 'save_post', 'retrieve_parent_order_on_purchase', 10, 3 );
function retrieve_parent_order_on_purchase( $post_id, $post, $update ) {
if ( $post->post_type == 'shop_order' && ! $update ) {
$parent_order = wc_get_order( $post_id );
// do something with $parent_order
}
}
I don't know if this works, but perhaps it can help? I'm curious to see if it works, if it doesn't let me know so I can delete this answer.
It's not clear what you mean by these 4 db calls
buy If you mean you want the function neworder_created
to be called after the order is created and saved in DB, you can use this hook woocommerce_checkout_order_created
instead of woocommerce_thankyou
hook.
It should be like that.
add_action('woocommerce_checkout_order_created', 'neworder_created', 10, 1);
That hook already passes the order object, so you need to change the function to.
function neworder_created($order){
$order_id = $order->get_id();
mainplugin($order_id, $order);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.