Php Hook Woodcommerce#u Thankyu don';我没有收到订单

Php Hook Woodcommerce#u Thankyu don';我没有收到订单,php,wordpress,woocommerce,product,orders,Php,Wordpress,Woocommerce,Product,Orders,在WooCommerce中,我正在使用一个挂接在WooCommerce\u thankyouaction钩子中的自定义函数,在付款后执行一些代码。钩子可以用,但我好像拿不到订单 这是简化的代码。从外观上看,未找到$order: add_action( 'woocommerce_thankyou', 'afterorder', 10, 1 ); function afterorder($order_id) { //$order = new WC_Order($order_id);

在WooCommerce中,我正在使用一个挂接在
WooCommerce\u thankyou
action钩子中的自定义函数,在付款后执行一些代码。钩子可以用,但我好像拿不到订单

这是简化的代码。从外观上看,
未找到$order

add_action( 'woocommerce_thankyou', 'afterorder', 10, 1 );


function afterorder($order_id) {
    //$order = new WC_Order($order_id);
    $order = wc_get_order($order_id);
    $order_items = $order->get_items();
    $order_comment_list = explode('\n', $order->customer_message);
    $payment_method = $order->payment_method_title; 

    foreach( $order_items as $product ) {
        $order->add_order_note('order for '.$product['name'].' received', false);
    }
}

我在这里遗漏了什么?

您的代码部分过时,并且自WooCommerce 3+以来出现了一些错误。订单行项目现在是
WC\u Order\u Item\u Product
类对象

对于订单“行项目”,您需要使用可用的方法获取相关数据,如相应的产品名称:

add_action( 'woocommerce_thankyou', 'afterorder', 10, 1 );
function afterorder( $order_id ) {
    // The WC_Order object
    $order = wc_get_order($order_id);

    $order_comment_list = explode( '\n', $order->get_customer_note() ); // Changed

    $payment_method = $order->get_payment_method_title(); // Changed 

    foreach( $order->get_items() as $line_item ) {
        // The WC_Product object
        $product = $line_item->get_product(); // Added
        $note = 'order for '.$product->get_title().' received';// Changed
        $order->add_order_note( $note, false );
    }
}
代码位于活动子主题(或活动主题)的function.php文件或任何插件文件中


您应该检查一下可能是
WC\u Order
方法,看看您是否正确地设置了它。

Nice!回答得很好。值得一提的是,
10,1
add\u操作的默认值,因此没有必要<代码>添加操作('woocommerce\u thankyou'、'afterorder')
…@cale\u b当编码更好地设置优先级和参数数量时,原因很多。这有点像严格的变量声明……但您是对的,这两种方法都适用。谢谢:)