gpt4 book ai didi

php - 从 Woocommerce 订单/WC_Order 对象获取订单备注?

转载 作者:行者123 更新时间:2023-12-03 06:22:40 24 4
gpt4 key购买 nike

我可以添加订单备注(私有(private)备注):

$order->add_order_note($info_for_order);

但是当我尝试使用以下方式获取某些页面中的值时:

get_comments(['post_id' => $order_id])
// or
$order_object->get_customer_order_notes()

它只是返回一个空数组。我用谷歌搜索了这个,但找不到执行此操作的方法。

最佳答案

Order notes (private note) are only available for backend when using get_comments() function.
If you look at WC_Comments exclude_order_comments() method you will see that front end queries are filtered regarding private order notes…

因此,转变是构建一个自定义函数来获取私有(private)订单备注:

function get_private_order_notes( $order_id){
global $wpdb;

$table_perfixed = $wpdb->prefix . 'comments';
$results = $wpdb->get_results("
SELECT *
FROM $table_perfixed
WHERE `comment_post_ID` = $order_id
AND `comment_type` LIKE 'order_note'
");

foreach($results as $note){
$order_note[] = array(
'note_id' => $note->comment_ID,
'note_date' => $note->comment_date,
'note_author' => $note->comment_author,
'note_content' => $note->comment_content,
);
}
return $order_note;
}

代码位于事件子主题(或主题)的 function.php 文件中或任何插件文件中。

此代码经过测试并且可以工作。

<小时/>

用法 (例如 $order_id = 6238 ):

$order_id = 6238;
$order_notes = get_private_order_notes( $order_id );
foreach($order_notes as $note){
$note_id = $note['note_id'];
$note_date = $note['note_date'];
$note_author = $note['note_author'];
$note_content = $note['note_content'];

// Outputting each note content for the order
echo '<p>'.$note_content.'</p>';
}

关于php - 从 Woocommerce 订单/WC_Order 对象获取订单备注?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43463217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com