简体   繁体   English

在购买时检索 woocommerce 父订单?

[英]Retrieving a woocommerce parent order on purchase?

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.现在我有一个挂钩,它会在 WooCommerce 订单完成和更新订单时运行。 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:当人们在我的网站上购买时,会发生以下情况:按创建顺序使用以下帖子类型进行 4 次数据库调用:

  • wc_booking (child) wc_booking(孩子)
  • shop_order (parent) shop_order(父级)
  • wcdp_payment (child) wcdp_payment(孩子)
  • wcdp_payment (child) wcdp_payment(孩子)

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;在 wcdp_payment 的最后一次数据库调用上运行,而不是我需要的; it should run only on that shop_order DB entry (or I guess the parent order is what I need) .它应该只在那个 shop_order 数据库条目上运行(或者我猜父订单是我需要的) wcdp_payment is missing order information. wcdp_payment 缺少订单信息。

Is there a way to get it to run when a post type is ONLY shop_order?当帖子类型只有 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?我知道我可以连接一些类似于我设置它来处理更新的逻辑,但我担心的是我不知道这个挂钩到底在做什么,所以它是否会到达所需的“shop_order”数据库条目? 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不清楚这 4 个 db 调用是什么意思

  • wc_booking (child) wc_booking(儿童)
  • shop_order (parent) shop_order(父)
  • wcdp_payment (child) wcdp_payment(孩子)
  • wcdp_payment (child) wcdp_payment(孩子)

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.购买 如果您的意思是希望在创建订单并将其保存在数据库中后调用函数neworder_created ,则可以使用此钩子woocommerce_checkout_order_created而不是woocommerce_thankyou钩子。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM