简体   繁体   English

在woocommerce_new_order挂钩中获取订单ID

[英]Get the order id in woocommerce_new_order hook

why don't can get id order in new order ? 为什么不能以新顺序获得id顺序?

This code don't work 该代码不起作用

add_action( 'woocommerce_new_order', 'get_order_details_woo',  1, 1  );
function get_order_details_woo ($order_id)
{

    $order = wc_get_order( $order_id );

    foreach ($order->get_items() as $item_key => $item_values):

        $item_id = $item_values->get_id();

        $item_data = $item_values->get_data();

        $product_name = $item_data['name'];

        //echo $product_name;
    endforeach;

}

But this code is work 但是这段代码是可行的

 add_action( 'woocommerce_new_order', 'get_order_details_woo',  1, 1  );

    function get_order_details_woo ()
    {

        $order = wc_get_order( 100);

        foreach ($order->get_items() as $item_key => $item_values):


            $item_data = $item_values->get_data();

            $product_name = $item_data['name'];

            //echo $product_name;
endforeach;

    }

How get id in new order ? 如何以新顺序获取ID?

Get new order details if i don't have order id ? 如果我没有订单编号,获取新的订单详细信息?

You get the order ID with this hook, but it seems that the order is just created at this moment with no product items yet. 您可以通过此钩子获得订单ID,但目前看来订单刚刚创建,尚无产品。 You can check this with var_dump. 您可以使用var_dump进行检查。

try another hook: 尝试另一个钩子:

add_action( 'woocommerce_checkout_create_order', 'get_order_details_woo',  1, 1  );

this one is fired just after the order object gets it's data and before it is saved to the database. 在订单对象获取数据之后以及将其保存到数据库之前,将触发此事件。 You don't have to retrieve the order ID here, because you get the order object directly. 您无需在此处检索订单ID,因为您可以直接获取订单对象。

add_action( 'woocommerce_checkout_update_order_meta', 'get_order_details_woo', 10, 2 ); add_action('woocommerce_checkout_update_order_meta','get_order_details_woo',10,2); function get_order_details_woo($order_id, $data) { } 函数get_order_details_woo($ order_id,$ data){}

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

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